vue之component中的template另一种定义方式

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .red{
            background:#FF0000;
        }
    </style>
</head>
<body>
<div id="app">
    <like></like>
</div>
<template id="like-component-tpl">
    <button :class="{red: liked}" @click="on_click">{{liked_count}}</button>
</template>
<script src="../lib/vue.js"></script>
<script src="./js/main.js"></script>
</body>
</html>

main.js:

Vue.component('like', {
    template: '#like-component-tpl',
    data: function(){
        return {
            liked_count: 10,
            liked: false
        }
    },
    methods: {
        on_click: function(){
            if(!this.liked){
                this.liked_count++;
                this.liked = true
            }else{
                this.liked_count--;
                this.liked = false;
            }
        }
    }
})

new Vue({
    el: '#app'
})

阅读更多
文章标签: vue component template
个人分类: vue
上一篇vue计算属性computed
下一篇vue中组件通信之父子通信:props(组件传参)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭