vue3.0之旅,每段代码用最新版vue 3.0.0-rc.2验证实操

5 篇文章 0 订阅
1 篇文章 0 订阅

1、实际运行一个简单项目,参考链接 掘金上一篇文章
按上面的步骤,下载了代码并实际运行了,运行过程中发现需要把原文中Vue.createApp().mount(App, '#app')改为Vue.createApp(App).mount('#app'),以上修改已经提交到github:https://github.com/pineappleok/vue-next
2、双向绑定的demo;

const { reactive } = Vue
var App = {
  template: `
    <div class="container">
         {{message}}
         <input v-model="name" />
         <p>{{name}}</p>
    </div>`,
  setup() {
    const state = reactive({
      message: 'Hello World!!!',
      name: ''
    })
    return {
      ...state
    }
  }
}
Vue.createApp(App).mount('#app')

3、子组件props、context参数;

const { reactive } = Vue
let Child = {
  // 属性定义
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  template: `<div>{{title}}</div>`,
  setup(props, context) {
    console.log(props);
    console.log(context);
  }
}
let App = {
  template: `
    <div class="container">
         {{message}}
         <input v-model="name" />
         <p>{{name}}</p>
         <Child title="test props two arguments"/>
    </div>`,
  components: { Child },
  setup() {
    const state = reactive({
      message: 'Hello World!!!',
      name: ''
    })
    return {
      ...state
    }
  }
}
Vue.createApp(App).mount('#app')

在这里插入图片描述
4、计数器 demo,toRefs的使用,

const { reactive, toRefs } = Vue
let Child = {
  // 属性定义
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  template: `<div>{{title}}</div>`,
  setup(props, context) {
    console.log(props)
    console.log(context)
  }
}
let App = {
  template: `
    <div class="container">
         {{message}}
         <input v-model="name" />
         <p>{{name}}</p>
         <Child title="test props two arguments"/>
         <h4>计数器 demo</h4>
         count: {{count}}
         <button @click="handlerCountAdd"> Click ++ </button>
    </div>`,
  components: { Child },
  setup() {
    const state = reactive({
      message: 'Hello World!!!',
      name: '',
      count: 0
    })
    const handlerCountAdd = () => {
      state.count++
    }
    return {
      ...toRefs(state),
      handlerCountAdd
    }
  }
}
Vue.createApp(App).mount('#app')

注意区别2双向绑定demo跟4计数器demo的区别,双向绑定demo的时候,其实不需要专门给name做toRefs处理,但是到计数器demo时,如果不给count做toRefs处理,那么可以在handlerCountAdd方法中打印得到最新的state中count已经递增,但是dom没有渲染出最新的count出来,还是显示count最原始的值0。

5、computed的引入与使用——反转字符串demo

const { reactive, toRefs, ref, computed } = Vue
let Child = {
  // 属性定义
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  template: `<div>{{title}}</div>`,
  setup(props, context) {
    console.log(props)
    console.log(context)
  }
}
let App = {
  template: `
    <div class="container">
         {{message}}
         <input v-model="name" />
         <p>{{name}}</p>
         <Child title="test props two arguments"/>
         <h4>计数器 demo</h4>
         count: {{count}}
         <button @click="handlerCountAdd"> Click ++ </button>
         <h4>ref使用:{{refExample}}</h4>  
         <h4>反转字符串demo</h4>
         <p>{{name}}反转后为:{{rName}}</p>       
    </div>`,
  components: { Child },
  setup() {
    const state = reactive({
      message: 'Hello World!!!',
      name: '',
      count: 0,
      rName: computed(() =>
        state.name
          .split('')
          .reverse()
          .join('')
      )
    })
    const handlerCountAdd = () => {
      state.count++
    }
    const refExample = ref('refExample')
    return {
      ...toRefs(state),
      handlerCountAdd,
      refExample
    }
  }
}
Vue.createApp(App).mount('#app')

6、watch的使用,监听器 watch 是一个方法,它包含 2 个参数,2 个参数都是 function

const { reactive, toRefs, ref, computed, watch } = Vue
let Child = {
  // 属性定义
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  template: `<div>{{title}}</div>`,
  setup(props, context) {
    console.log(props)
    console.log(context)
  }
}
let App = {
  template: `
    <div class="container">
         {{message}}
         <input v-model="name" />
         <p>{{name}}</p>
         <Child title="test props two arguments"/>
         <h4>计数器 demo</h4>
         count: {{count}}
         <button @click="handlerCountAdd"> Click ++ </button>
         <h4>ref使用:{{refExample}}</h4>  
         <h4>反转字符串demo</h4>
         <p>{{name}}反转后为:{{rName}}</p>       
    </div>`,
  components: { Child },
  setup() {
    const state = reactive({
      message: 'Hello World!!!',
      name: '',
      count: 0,
      rName: computed(() =>
        state.name
          .split('')
          .reverse()
          .join('')
      ),
      countDouble: computed(() =>
        state.count*2
      ),
      countTriple: computed(() =>
        state.count*3
      ),
      countQuadruple: computed(() =>
        state.count*4
      ),
    })
    const handlerCountAdd = () => {
      state.count++
    }
    const refExample = ref('refExample')
    watch(() => [state.count, state.countDouble, state.countTriple, state.countQuadruple], (val, preVal) => {
      console.log('watch val==', val);
      console.log('watch preVal==', preVal);
    })
    return {
      ...toRefs(state),
      handlerCountAdd,
      refExample
    }
  }
}
Vue.createApp(App).mount('#app')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值