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'
})

Logo

前往低代码交流专区

更多推荐