一、 不使用构建工具
1. 导入vue.js的script 脚本文件
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
2. 在页面中声明一个将要被vue所控制的DOM区域,即MVVM中的view
<div id="app">
{{ message }}
</div>
3. 创建vm实例对象(vue实例对象)
<script>
const { createApp } = Vue
createApp({
data() {
return {
message: 'Hello Vue2!'
}
}
}).mount('#app')
</script>
4. 完整代码(跟上面写法不同,但是意思一样)
<!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="app">
{{ message }}
</div>
<script>
const hello ={
data:function() {
return {
message: 'Hello Vue!'
}
}
}
const app=Vue.createApp(hello);
app.mount('#app')
</script>
</body>
</html>
二、内容渲染指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<p>姓名: {{ username }}</p>
<p>性别: {{ gender }}</p>
<p>{{ desc }}</p>
<p v-html="desc"></p>
</div>
<script>
const vm={
data:function(){
return {
username:'张阿三',
gender:'男',
desc:'<a href="https://www.baidu.com/">百度</a>',
}
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
三 、属性绑定指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<a :href="link">百度</a>
<input type="text" :placeholder="inputValue">
<img :src="imgSrc" :style="{width: w}" alt="">
</div>
<script>
const vm={
data:function(){
return {
link:'https://www.baidu.com',
inputValue:'请输入数据',
imgSrc: './image/demo.png',
w: '500px',
}
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
四、使用Javascript表达式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<p>{{number+1}}</p>
<p>{{ok ? 'True ':'False'}}</p>
<p>{{message.split('').reverse().join('')}}</p>
<p :id="'list-'+id">xxx</p>
<p>{{user.name}}</p>
</div>
<script>
const vm={
data:function(){
return {
number:11,
ok:true,
message: 'ABC',
id: 3,
user:{
name:'zs',
}
}
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
五、事件绑定指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<h3>count的值{{count}}</h3>
<button v-on:click="addCount">+1</button>
<button @click="count+=1">+1</button>
</div>
<script>
const vm={
data:function(){
return {
count:0,
}
},
methods:{
addCount(){
this.count+=1
},
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
六、 条件渲染指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<button @click="flag=!flag">Toggle Flag</button>
<p v-if="flag">请求成功--- 被v-if控制</p>
<p v-show="flag">请求成功--- 被v-show控制</p>
</div>
<script>
const vm={
data:function(){
return {
flag:false,
}
},
methods:{
addCount(){
this.count+=1
},
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
七、 v-else和v-else-if指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<p v-if="num>0.5">随机数>0.5</p>
<p v-else>随机数<=0.5</p>
<hr/>
<p v-if="tyep=='A'">优秀</p>
<p v-else-if="tyep=='B'">良好</p>
<p v-else-if="tyep=='C'">一般</p>
<p v-else>极差</p>
<div v-show="a">
测试
</div>
<button @click="a=!a">点击</button>
</div>
<script>
const vm={
data:function(){
return {
a:false,
num:Math.random(),
tyep:'B'
}
},
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
八、列表渲染指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<ul>
<li v-for="(user,i) in userList ">索引是:{{i}},姓名是:{{user.name}}</li>
</ul>
</div>
<script>
const vm={
data:function(){
return {
userList:[
{ id: 1,name:'zhangsansan'},
{ id: 2,name:'lisi'},
{ id: 3,name:'wangwu'}
]
}
},
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>
九、 v-for中的key
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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="app">
<div>
<input type="text" v-model="name">
<button @click="addNewUser">添加</button>
</div>
<ul>
<li v-for="(user,index) in userList " :key="user.id">
<input type="checkbox" >
姓名是:{{user.name}}
</li>
</ul>
</div>
<script>
const vm={
data:function(){
return {
userList:[
{ id: 1,name:'zhangsansan'},
{ id: 2,name:'lisi'}
],
name:'',
nextId:3
}
},
methods:{
addNewUser(){
this.userList.unshift({ id: this.nextId,name:this.name})
this.name=''
this.nextId++
},
}
}
const app=Vue.createApp(vm);
app.mount('#app')
</script>
</body>
</html>