vue3--setup

<template>
  <h1>一个人的信息</h1>
  <h2>姓名:{{name}}</h2>
  <h2>年龄:{{age}}</h2>
  <h2>性别:{{sex}}</h2>
  <h2>a的值:{{a}}</h2>
  <button @click="sayHello">说话(vue3所配置的--sayHello)</button>
  <br> 
  <br> 
  <button @click="sayWelcom">说话(vue2所配置的--sayWelcom)</button>
  <br> 
  <br> 
  <button @click="test1">测试一下在vue2的配置中去读取Vue3的数据、方法</button>
  <br> 
  <br> 
  <button @click="test2">测试一下在vue3的配置中去读取Vue2的数据、方法</button>
</template>

vue2中值与方法的使用

<script>
// import {h} from 'vue'
export default{
  name:'App',
  data(){
    return {
      sex:'男',
      // a:100
    }
  },
  methods:{
    sayWelcom() {
      alert('欢迎')
    },
    test1() {
      console.log(this.sex);
      console.log(this.name);
      console.log(this.age);
      console.log(this.sayHello);
      this.sayHello()
    }
  }
}
</script>

vue3

<script>
// import {h} from 'vue'
export default{
  name:'App',
  // 此处是测试一下 setup 暂时不考虑响应式的问题
  setup(){
    // 数据
    let name = '张三'
    let age = 18
    let a = 200

    // 方法
    function sayHello(){
      alert('我叫'+name+',我'+age+'岁了,你好啊')
      // ``
    }
    function test2() {
      console.log(name);
      console.log(age);
      console.log(this.sex);
      console.log(this.sayWelcom);
      // this.sayHello()
    }

    return {
      name,
      age,
      a,
      sayHello,
      test2
    }

    // 返回一个函数(渲染函数)
    // return  () => {
    //   return h('h1','25')
    // }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值