Vue是一个用于构建用户界面(基于数据构建出用户能够看懂的界面)的渐进式框架
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 准备容器 -->
<div id="app">
<!-- 放置用于渲染的代码逻辑 -->
{{msg}}
</div>
<!-- 引包 -->
<!-- 去官网https://v2.cn.vuejs.org/v2/guide/installation.html -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<!-- 创建实例 -->
<script>
// 一旦引入VueJS核心包,在全局环境下,就有了Vue函数
const app=new Vue({
// 通过el配置选择器,指定管理哪个容器
el:'#app',
// 通过data提供渲染数据
data:{
msg:"hello world!"
}
})
</script>
</body>
</html>
{{}}
const app=new Vue({
// 通过el配置选择器,指定管理哪个容器
el:'#app',
// 通过data提供渲染数据
data:{
msg:"hello world!"
}
})
带有v- 前缀的特殊标签
在data中使用标签时,不能直接显示,此时就可以用Vue指令
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<div id="app">
<p v-if="judge === true" >姓名:小明p>
<p v-else="judge">小红p>
等级:
<p v-if="score >= 90">优秀p>
<p v-else-if="score >= 70">良好p>
<p v-else-if="score >= 60">及格p>
<p v-else="score >= 90">不及格p>
div>
<script>
const p = new Vue({
el:"#app",
data:{
judge:true,
score:85
}
})
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>Documenttitle>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<div id="app">
<Button v-on:click = "num--">-Button>
<label>{{num}}label>
<Button v-on:click = "num++">+Button>
div>
<script>
const p = new Vue({
el:"#app",
data:{
num:100
}
})
script>
body>
html>
- v-on :事件名="methods中的函数名"
- eg:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<div id="app">
<Button @click = "dec">-Button>
<label>{{num}}label>
<Button @click = "inc">+Button>
div>
<script>
const p = new Vue({
el:"#app",
data:{
num:100
},
methods:{
// 通过变量名获取变量
inc(){
p.num++;
},
dec(){
p.num--;
}
}
})
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>Documenttitle>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<table id="price" border="1px">
<tr>
<th>商品th>
<th>价格(元)th>
<th>操作th>
tr>
<tr>
<td>薯片td>
<td>{{sPrice}}td>
<td><button @click="dec(sPrice)">+button>td>
tr>
<tr>
<td>饮料td>
<td>{{yPrice}}td>
<td><button @click="dec(yPrice)">+button>td>
tr>
<tr>
<td>包子td>
<td>{{bPrice}}td>
<td><button @click="dec(bPrice)">+button>td>
tr>
table>
<div id="sum">
<p>余额:{{money}}p>
div>
<script>
const sum=new Vue({
el:"#sum",
data:{
money:100
}
})
const p = new Vue({
el:"#price",
data:{
sPrice:4,
yPrice:3,
bPrice:2,
},
methods:{
dec(price){
sum.money -= price;
}
}
})
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>Documenttitle>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
head>
<body>
<div id="app">
<img v-bind:src="msg" alt="" >
<img :src="msg" alt="">
div>
<script>
const app = new Vue({
el:"#app",
data:{
msg:"https://img2.baidu.com/it/u=2728121553,3700197055&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
}
})
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>Documenttitle>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<div id="p" >
<p v-for="item in msg">{{item}}p>
div>
<script>
const p = new Vue({
el:"#p",
data:{
msg:["hello,world!","你好世界","世界你好"]
}
})
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>Documenttitle>
head>
<body>
<div id="model">
账户:<input type="text" v-model="username"><br/><br/>
密码:<input type="password" v-model="password"><br/>
div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<script>
const p = new Vue({
el:"#model",
data:{
username:'',
password:''
}
})
script>
body>
html>