小言_互联网的博客

Vue2和Vue3的区别

864人阅读  评论(0)

1. vue2和vue3双向数据绑定原理发生了改变

vue2 的双向数据绑定是利用ES5 的一个 API Object.definePropert()对数据进行劫持 结合 发布订阅模式的方式来实现的。

vue3 中使用了 es6 的 ProxyAPI 对数据代理。

相比于vue2.x,使用proxy的优势如下

  1. defineProperty只能监听某个属性,不能对全对象监听

  1. 可以省去for in、闭包等内容来提升效率(直接绑定整个对象即可)

  1. 可以监听数组,不用再去单独的对数组做特异性操作 vue3.x可以检测到数组内部数据的变化

2. Vue3支持碎片(Fragments)

就是说在组件可以拥有多个根节点。

3. Composition API

vue2

   
  1. export default {
  2. props: {
  3. title: String
  4. },
  5. data () {
  6. return {
  7. username: '',
  8. password: ''
  9. }
  10. },
  11. methods: {
  12. login () {
  13. // 登陆方法
  14. }
  15. },
  16. components:{
  17. "buttonComponent":btnComponent
  18. },
  19. computed:{
  20. fullName(){
  21. return this.firstName+" "+this.lastName;
  22. }
  23. }
  24. }

vue3


   
  1. export default {
  2. props: {
  3. title: String
  4. },
  5. setup () {
  6. const state = reactive({ //数据
  7. username: '',
  8. password: '',
  9. lowerCaseUsername: computed(() => state.username.toLowerCase()) //计算属性
  10. })
  11. //方法
  12. const login = () => {
  13. // 登陆方法
  14. }
  15. return {
  16. login,
  17. state
  18. }
  19. }
  20. }

4. 建立数据 data

Vue2 - 这里把数据放入data属性中


   
  1. export default {
  2. props: {
  3. title: String
  4. },
  5. data () {
  6. return {
  7. username: '',
  8. password: ''
  9. }
  10. }
  11. }

在Vue3.0,我们就需要使用一个新的setup()方法,此方法在组件初始化构造的时候触发。

使用以下三步来建立反应性数据:

从vue引入reactive

使用reactive()方法来声名我们的数据为响应性数据

使用setup()方法来返回我们的响应性数据,从而我们的template可以获取这些响应性数据

5. 生命周期钩子

  1. 父子传参不同,setup() 函数特性

总结:

1、setup 函数时,它将接受两个参数:(props、context(包含attrs、slots、emit))

2、setup函数是处于 生命周期函数 beforeCreate 和 Created 两个钩子函数之前的函数

3、执行 setup 时,组件实例尚未被创建(在 setup() 内部,this 不会是该活跃实例的引用,即不指向vue实例,Vue 为了避免我们错误的使用,直接将 setup函数中的this修改成了 undefined)

4、与模板一起使用:需要返回一个对象 (在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用)

5、使用渲染函数:可以返回一个渲染函数,该函数可以直接使用在同一作用域中声明的响应式状态

注意事项:

1、setup函数中不能使用this。Vue 为了避免我们错误的使用,直接将 setup函数中的this修改成了 undefined)

2、setup 函数中的 props 是响应式的,当传入新的 prop 时,它将被更新。但是,因为 props 是响应式的,你不能使用 ES6 解构,因为它会消除 prop 的响应性。

如果需要解构 prop,可以通过使用 setup 函数中的toRefs 来完成此操作:

父传子,props


   
  1. import { toRefs } from 'vue'
  2. setup(props) {
  3. const { title } = toRefs(props)
  4. console.log(title.value)
  5. onMounted(() => {
  6. console.log('title: ' + props.title)
  7. })
  8. }

子传父,事件 - Emitting Events

7. vue3 Teleport瞬移组件

Teleport一般被翻译成瞬间移动组件,实际上是不好理解的.我把他理解成"独立组件",

他可以那你写的组件挂载到任何你想挂载的DOM上,所以是很自由很独立的

以一个例子来看:编写一个弹窗组件


   
  1. <template>
  2. <teleport to="#modal">
  3. <div id="center" v-if="isOpen">
  4. <h2><slot>this is a modal</slot></h2>
  5. <button @click="buttonClick">Close</button>
  6. </div>
  7. </teleport>
  8. </template>
  9. <script lang="ts">
  10. export default {
  11. props: {
  12. isOpen: Boolean,
  13. },
  14. emits: {
  15. 'close-modal': null
  16. },
  17. setup(props, context) {
  18. const buttonClick = () => {
  19. context.emit('close-modal')
  20. }
  21. return {
  22. buttonClick
  23. }
  24. }
  25. }
  26. </script>
  27. <style>
  28. #center {
  29. width: 200px;
  30. height: 200px;
  31. border: 2px solid black;
  32. background: white;
  33. position: fixed;
  34. left: 50%;
  35. top: 50%;
  36. margin-left: -100px;
  37. margin-top: -100px;
  38. }
  39. </style>

在app.vue中使用的时候跟普通组件调用是一样的


   
  1. <template>
  2. <div id="app">
  3. <img alt="Vue logo" src="./assets/logo.png">
  4. <HelloWorld msg="Welcome to Your Vue.js App"/>
  5. <HooksDemo></HooksDemo>
  6. <button @click="openModal">Open Modal</button><br/>
  7. <modal :isOpen="modalIsOpen" @close-modal="onModalClose"> My Modal !!!!</modal>
  8. </div>
  9. </template>
  10. <script>
  11. import HelloWorld from './components/HelloWorld.vue'
  12. import HooksDemo from './components/HooksDemo.vue'
  13. import Modal from './components/Modal.vue'
  14. import{ref} from 'vue'
  15. export default {
  16. name: 'App',
  17. components: {
  18. HelloWorld,
  19. HooksDemo,
  20. Modal
  21. },
  22. setup() {
  23. const modalIsOpen = ref(false)
  24. const openModal = () => {
  25. modalIsOpen.value = true
  26. }
  27. const onModalClose = () => {
  28. modalIsOpen.value = false
  29. }
  30. return {
  31. modalIsOpen,
  32. openModal,
  33. onModalClose
  34. }
  35. }
  36. }
  37. </script>
  38. <style>
  39. #app {
  40. font-family: Avenir, Helvetica, Arial, sans-serif;
  41. -webkit-font-smoothing: antialiased;
  42. -moz-osx-font-smoothing: grayscale;
  43. text-align: center;
  44. color: #2c3e50;
  45. margin-top: 60px;
  46. }
  47. </style>

要是在app.vue文件中使用的时候,modal是在app的 DOM节点之下的,父节点的dom结构和css都会给modal产生影响

于是产生的问题

modal被包裹在其它组件之中,容易被干扰

样式也在其它组件中,容易变得非常混乱

Teleport 可以把modal组件渲染到任意你想渲染的外部Dom上,不必嵌套在#app中,这样就可以互不干扰了,可以把Teleport看成一个传送门,把你的组件传送到任何地方

使用的时候 to属性可以确定想要挂载的DOM节点下面


   
  1. <template>
  2. <teleport to="#modal">
  3. <div id="center">
  4. <h2>柏特better</h2>
  5. </div>
  6. </teleport>
  7. </template>

在public文件夹下的index.html中增加一个节点


   
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  7. <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  8. <title><%= htmlWebpackPlugin.options.title %></title>
  9. </head>
  10. <body>
  11. <noscript>
  12. <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  13. </noscript>
  14. <div id="app"></div>
  15. <div id="modal"></div>
  16. <!-- built files will be auto injected -->
  17. </body>
  18. </html>

转载:https://blog.csdn.net/m0_62877814/article/details/128935510
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场