demo.html
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"
xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
{{message}}
<div>
<span v-bind:title="message">显示title提示信息</span>
</div>
<div v-if="ok">yes</div>
<div v-else>No</div>
<li v-for="item in items">
{{item.message}}
</li>
<div>
<button v-on:click="sayHi()">click me</button>
</div>
<div>
<div>
输入数据双向绑定:<input type="text" v-model="message"/>
</div>
<div>
性别:
<input type="radio" name="sex" value="男" v-model="checkedValue">男
<input type="radio" name="sex" value="女" v-model="checkedValue">女
<p>选中了:{{checkedValue}}</p>
</div>
<div>
喜欢的水果:
<input type="checkbox" name="fruit" value="苹果" v-model="checkedFruits">苹果
<input type="checkbox" name="fruit" value="梨子" v-model="checkedFruits">梨子
<p>选中了:<span v-for="item in checkedFruits">{{item}}</span></p>
</div>
<div>
<select v-model="selectedValue">
<option value="" disabled>--请选择--</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<p>选中了:{{selectedValue}}</p>
</div>
</div>
<div>
<diycomponent v-for="(item,index) in items" v-bind:diyargs="item.message"></diycomponent>
</div>
<div>
<div>账户:{{userInfo.username}}</div>
<div>密码:{{userInfo.password}}</div>
</div>
<div>
<p>currentTime1:{{getTime1()}}</p>
<p>currentTime2:{{getTime2}}</p>
</div>
<div>
<slot-box>
<slot-title slot="slot-title" :title="message"></slot-title>
<slot-item slot="slot-item" v-for="(item,index) in items"
:item="item.message"
v-on:remove="removeItem(index)"
:key="index">
</slot-item>
</slot-box>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
Vue.component("diycomponent",
{
props: ['diyargs'],
template: '<li>{{diyargs}}</li>'
});
Vue.component("slot-box", {
template: " <div>\n" +
" <slot name='slot-title'></slot>\n" +
" <ul>\n" +
" <slot name='slot-item'></slot>\n" +
" </ul>\n" +
" </div>"
});
Vue.component("slot-title", {
props: ['title'],
template: "<div>{{title}}</div>"
});
Vue.component("slot-item", {
props: ['item', 'index'],
template: "<li>{{index}}---{{item}}<button @click='remove'>删除</button></li>",
methods: {
remove: function (index) {
this.$emit('remove', 'index');
alert("删除了" + index);
}
}
})
var vm = new Vue({
el: "#app",
data() {
return {
message: "hello,vue!",
ok: true,
items: [{message: '输出信息1'},
{message: '输出信息2'},
{message: '输出信息3'}
],
checkedValue: "",
checkedFruits: [],
selectedValue: "",
userInfo: {
username: null,
password: null
}
}
},
mounted() {
axios.get('data.json').then(response=>(this.userInfo=response.data));
},
methods: {
sayHi: function (event) {
alert(this.message);
},
getTime1: function () {
return Date.now();
},
removeItem: function (index) {
this.items.splice(index, 1);
}
},
computed: {
getTime2: function () {
return Date.now();
}
}
});
</script>
</body>
</html>
data.json
{"username":"root","password":"123456"}