小编之前的文章都是js和ts根底,根底完事之后,是时候向前端的支流框架发动挑战了,接下来小编次要针对当初比拟风行的Vue3联合实例,和大家一起探讨一下Vue的根本语法和Vue3中提供的一些新个性,心愿小编能够和大家一起成长,一起在前端的路上,越走越远。
咱们常见的html模板,根本都是这样的
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> </body></html>
依照咱们之前的常识储备,想应用Vue.js,必定要引入对应的js文件,这时候,咱们的文件就变成了这样
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://unpkg.com/vue@next"></script><!-- 能够依据理论文件门路引入 --></head><body> </body></html>
咱们能够在代码中编写这样的代码,在页面中展现hello world
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://unpkg.com/vue@next"></script></head><body> <div id="root"></div></body><script>Vue.createApp({ template:'<div>hello world</div>' // 定义模板}).mount("#root") // 设置挂载点</script></html>
当然,咱们还能够把代码写的更Vue一点,就像这样
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://unpkg.com/vue@next"></script></head><body> <div id="root"></div></body><script>Vue.createApp({ data(){ return { content:'hello world' } }, template:'<div>{{content}}</div>' // 定义模板}).mount("#root") // 设置挂载点</script></html>
可能大家看到这样奇奇怪怪的代码,会有一些蒙圈,不过明天只是相熟相熟,后续小编会把这些问题一一解释分明。上面就是Counter。实现的就是在每一秒中,页面上的数字加一,借助Vue,代码能够写成这样
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://unpkg.com/vue@next"></script></head><body> <div id="root"></div></body><script>Vue.createApp({ data(){ return { content:1 } }, mounted(){ // 生命周期函数,后续会专门阐明 setInterval(() => { // es6中的箭头函数 this.content += 1 // this指向Vue实例 },1000) }, template:'<div>{{content}}</div>'}).mount("#root")</script></html>
以上就是借助Vue.js来实现的两个根本基本功能,大家可能和小编一开始接触Vue一样懵圈,不过不要紧,置信通过一段时间的相熟,肯定能够有个质的晋升,一起加油!
大家还能够扫描二维码,关注我的微信公众号,蜗牛全栈。