网址:https://scrimba.com/p/pXKqta/cB4gESW
主要记一些之前没见过的标签,操作:
1.首先一个例子,包含三个文件
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{ message }}
</div>
<script src="index.js"></script>
</body>
</html>
index.css
html, body {
margin: 5px;
padding: 0;
}
index.js
var app = new Vue({
el: '#app', //这里的标签可以定位index中对应的内容
data: {
message: 'Hello Vue!'
}
});
//app.message='可以在这里修改Hello Vue的内容哦'
2.学习用v-if属性
其中index.js不变还是那个//之后无特殊说明都是这个
index.html改为
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<span v-if="seen">Now you see me</span>
</div>
<script src="index.js"></script>
</body>
</html>
index.js
var app=new Vue({
el:'#app',
data:{
seen:true;
}
})
//app.seen=false; //可以用这句话隐藏now you see me
3.v-for属性
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<li v-for="todo in todos">
{{todo.text}}
</li>
</ol>
</div>
<script src="index.js"></script>
</body>
</html>
index.js
var app=new Vue({
el:'#app',
data:{
todos:[
{text:'Learn Javascript'},
{text:'Learn Vue'},
{text:'Do something awesome'}
]
}
})
app.todos.push({text:'new items'});//这句是加一些新的项
4.v-on属性(与用户交互相关)
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{message}}</p>
<button v-on:click="reverseMessage">reverse messages</button>
</div>
<script src="index.js"></script>
</body>
</html>
index.js
var app=new Vue({
el:'#app',
data:{
message:'Hello Vue.js!'
},
methods:{
reverseMessage:function(){
this.message=this.message.split('').reverse().join('')
}
}
})
5.v-model属性 (绑定制定块,input中修改内容反映到message中)
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{message}}</p>
<input v-model="message">
</div>
<script src="index.js"></script>
</body>
</html>
index.js
var app=new Vue({
el:'#app',
data:{
message:"Hello Vue!"
}
})
6.component 用法
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<todo-item></todo-item>
<todo-item></todo-item>
</ol>
</div>
<script src="index.js"></script>
</body>
</html>
index.js
Vue.component('todo-item',{
template:'<li>This is a todo</li>'
})
var app=new Vue({
el:'#app'
})
7.component with props
index.html
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<todo-item
v-for="item in groceryList"
v-bind:todo="item"
v-bind:key="item.id">
</todo-item>
</ol>
</div>
<!-- https://vuejs.org/v2/guide/list.html#key -->
<script src="index.js"></script>
</body>
</html>
index.js
Vue.component('todo-item', {
props: ['todo'],
template: '<li>{{ todo.text }}</li>'
})
var app = new Vue({
el: '#app',
data: {
groceryList: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
]
}
})