2019-10-19
- 監(jiān)聽(tīng)路由
watch: {
$route(newRoute,oldRoute) {
// todo
}
}
//實(shí)例
let vm = new Vue({
el: "#app",
data: {},
router,
watch: {
'$route.path': function (newVal, oldVal) {
if (newVal === '/login') {
console.log('歡迎進(jìn)入登錄頁(yè)面');
}
if (newVal === '/register') {
console.log('歡迎進(jìn)入注冊(cè)頁(yè)面');
}
}
}
})
watch監(jiān)聽(tīng)路由.png
- 深層監(jiān)聽(tīng)
watch:{
childrens:{
handler:function(val,oldval){
console.log(val.name)
},
deep:true//對(duì)象內(nèi)部的屬性監(jiān)聽(tīng)括眠,也叫深度監(jiān)聽(tīng)
},
}
- 立即執(zhí)行
poster: {
handler(newName, oldName) {
// ...
},
immediate: true
}
Vue.js 簡(jiǎn)單購(gòu)物車(chē)-(例子)
//html文件----------------------------------------------------------------------------------------------------------------------
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
<table>
<tr>
<th>序號(hào)</th>
<th>商品名稱(chēng)</th>
<th>商品價(jià)格</th>
<th>購(gòu)買(mǎi)數(shù)量</th>
<th>操作</th>
</tr>
<tr v-for="iphone in Ip_Json">
<td>{{ iphone.id }}</td>
<td>{{ iphone.name }}</td>
<td>{{ iphone.price }}</td>
<td>
<button v-bind:disabled="iphone.count === 0" v-on:click="iphone.count-=1">-</button>
{{ iphone.count }}
<button v-on:click="iphone.count+=1">+</button>
</td>
<td>
<button v-on:click="iphone.count=0">移除</button>
</td>
</tr>
</table>
總價(jià):${{totalPrice()}}
</div>
//css文件----------------------------------------------------------------------------------------------------------------------
table {
border: 1px solid black;
}
table {
width: 100%;
}
th {
height: 50px;
}
th, td {
border-bottom: 1px solid #ddd;
}
//js文件
var app = new Vue({
el: '#app',
data: {
Ip_Json: [{
id: 1,
name: 'iphone 8',
price: 5099,
count: 1
},
{
id: 2,
name: 'iphone xs',
price: 8699,
count: 1
},
{
id: 3,
name: 'iphone xr',
price: 6499,
count: 1
}]
},
methods:{
totalPrice : function(){
var totalP = 0;
for (var i = 0,len = this.Ip_Json.length;i<len;i++) {
totalP+=this.Ip_Json[i].price*this.Ip_Json[i].count;
}
return totalP;
}
}
})