VUE3.0从安装到应用 (3)

生命周期

vue2.x
router全局引入在文章2里面有

import home from './home .vue'
export default {
  ...applying,
  name: "applying",
  data() {
    return {
		ifShow:true
    }
  },
  mounted() {},
  created(){

  },
  methods:{},
}

vue3
vue文件

<div>首页</div>
<script>
  import home from "./home.ts";
  export default {
    setup() {
	  let {showV,helloEvent} = home();
      return {
        showV,helloEvent
      }
	}
  }
</script>

ts文件

import { onMounted, ref, nextTick } from "vue"
export default function home() {
    const lArr = ref([]);
    const inputValue = ref();
    const showV = ref(true);
    const video = ref(null);
    onMounted(async () => {

    });
    const helloEvent = () => {
		alert(1)
    };
    return {
        showV,
        helloEvent,
    }
}

vue2里
在这里插入图片描述
引入vue然后进行声明,在data()里命名变量,method,mounted,created写方法.

vue3里按需引入 onMounted就和mounted一个作用,然后
在这个方法里面命令变量,写方法,然后 return输出,
在vue文件里引入ts,在setup()方法里引入刚才写的方法。
在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用。

之前vue模板所有标签都得放到一个标签下,vue3中template模板可以放多个标签。

我这样写是为了区分页面和js,也可以直接在页面写!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值