当前位置:网站首页>5.el与data的两种写法
5.el与data的两种写法
2022-07-19 05:05:00 【风落不归处】
1.data与el的2种写法
(1)el有2种写法
<1> new Vue的时候配置el属性
<2> 先创建Vue实例,然后再通过vm.$mount("#root")指定el的值
(2)data有2种写法
<1> 对象式(不使用脚手架时均可用,使用脚手架时,在vue2中可用于vm上,不
能用于vc上,在vue3中,vm和vc均不可用)
<2> 函数式(任何场景均可用)
注意:如何选择data,不使用vue-cli时,哪种写法都可,使用vue-cli时,尽量使用
函数式,否则在vm中会报错。
(3)一个重要的原则:
由Vue管理的函数,一定不要写箭头函数,一旦写了箭头函数,this就不再是
vue实例了。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>el与data的两种写法</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器-->
<div id="root">
<h1>你好,{
{name}}</h1>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
//el的两种写法
/* const v = new Vue({
//el:'#root', //第一种写法
data:{
name:'尚硅谷'
}
})
v.$mount('#root') //第二种写法 */
//data的两种写法
new Vue({
el:'#root',
//data的第一种写法:对象式
/* data:{
name:'尚硅谷'
} */
//data的第二种写法:函数式
data(){
console.log('@@@',this) //此处的this是Vue实例对象
return{
name:'尚硅谷'
}
}
})
</script>
</html>
边栏推荐
- 【学习笔记】Unreal(虚幻)4引擎入门(四)
- What if the game needs to be reinstalled after the steam folder is moved
- 更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
- 长篇详解C语言动态内存管理
- The longest common subsequence of order 2 and order n
- Character functions and string functions
- C language custom types: structure, enumeration, union
- Shell Scripting
- 动态内存申请
- C语言中的文件操作
猜你喜欢
Interval coverage problem
C language custom types: structure, enumeration, union
Pytorch target detection competition (I) data analysis
Chapter 62: blue screen crash when running vs program on win10
数据在内存中存储是怎样的?
c语言指针重难点
通过调试发现程序死循环的原因
Custom type: structure, bit segment, enumeration, union
轻松掌握|struct结构体|知识点
Sanziqi (n-ziqi) C language programming, super detailed explanation
随机推荐
学习日记-指针专题
节流阀中的 flag=false if(flag){return} timer=null if(timer){return} 写法一直不太明白怎么起作用的
Obsidian compiles third-party plug-ins
4000字,让你明白递推及其例题做法(C语言,有图)
Function recursion in C program
轻松掌握|C语言字符串与内存的相关库函数|
Three piece chess game
淘宝flexible.js文件实现弹性布局
Dynamic memory management
ZABBIX server Ping link monitoring, alarm by email after status change
Data tower problem and deformation
Recursive backtracking - maze walking
结构体专题
冒泡排序思想及实现
C language program environment and preprocessing
字符串函数和内存操作函数
第七十四篇:机器学习优化方法及超参数设置综述
【学习笔记】Unreal(虚幻)4引擎入门(三)
数据在内存中存储是怎样的?
Application of learning diary 5-c language function