react & vue & angularvuealready

vue -- 指令 -- v-text/html/on/show

2022-07-21  本文已影响0人  像我这么帅的一般都是主角

v-text

    <div id="app">
        <h2 v-text="message"></h2>
        <h2 v-text="info"></h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                message: '11111',
                info:'lalala'
            }
        })
    </script>
image.png

v-html

    <div id="app">
        <h2 v-html="content"></h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                // content:'programmer'
                content:'<a href="http://www.bilibili.com">programmer</a>'
            }
        })
    </script>

v-on

<body>
    <div id="app">
        <input type="button" value="v-on指令" v-on:click="doIt">
        <input type="button" value="v-on简写" @click="doIt">
        <input type="button" value="double click" @dblclick="doIt">
        <h2 @click="changeFood">{{ food }}</h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data: {
                food: 'hot pot'
            },
            methods:{
                doIt:function () {
                    alert(' do it ');
                },
                changeFood:function () {
                    this.food += ' delicious'
                    console.log(this.food)
                }
            }
        })
    </script>
</body>

v-on补充

<body>
    <div id="app">
        <input type="button" value="click" @click="doIt(666,'lao tie')">
        <input type="text" @keyup.enter="sayHi">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                arr:['beijing','shanghai','guangzhou','shenzhen'],
                food:[
                    {name: 'hot pot'},
                    {name: 'hot dry noodles'}
                ]
            },
            methods: {
                doIt:function (p1,p2) {
                    console.log('do it')
                    console.log(p1)
                    console.log(p2)
                },
                sayHi:function () {
                    alert('岂料毛')
                }
            }
        })
    </script>
</body>

实例 - 计数器


image.png
<body>
    <div id="app">
        <div class="input-num">
            <button @click="sub">
                -
            </button>
            <span>{{ num }}</span>
            <button @click="add">
                +
            </button>
        </div>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data: {
                num: 1
            },
            methods:{
                add:function () {
                    if(++(this.num) > 10){
                        this.num = 10
                        alert('超过最大值')
                    }
                },
                sub:function () {
                    if(--(this.num) < 0){
                        this.num = 0
                        alert('超过最小值')
                    }
                }
            }
        })
    </script>
</body>

v-show

<div id="app">
        <input type="button" value="display" @click="changeIsShow">
        <img v-show="isShow" src="img/read.png" alt="">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                isShow:false,
                age:18
            },
            methods:{
                changeIsShow:function(){
                    this.isShow = ! this.isShow
                }
            }

        })
    </script>

v-if

<body>
    <div id="app">
        <input type="button" value="toggle" @click="toggleIsShow">
        <p v-if="isShow">programmer</p>
        <p v-show="isShow">programmer *</p>
        <h2 v-if="temperature>=35">热死了</h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                isShow:false,
                temperature:40
            },
            methods:{
                toggleIsShow:function(){
                    this.isShow = ! this.isShow
                }
            }

        })
    </script>
</body>

v-bind

<body>
    <div id="app">
        <img :src="imgSrc" alt="" v-bind:title="imgTitle">
        <img :src="imgSrc" alt="" v-bind:title="imgTitle" :class="isActive?'active':''" @click="toggleActive">
        <img :src="imgSrc" alt="" v-bind:title="imgTitle" :class="{active:isActive}" @click="toggleActive">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                imgSrc:'img/read.png',
                imgTitle:'read',
                isActive:false
            },
            methods:{
                toggleActive:function(){
                    this.isActive = ! this.isActive
                }
            }
        })
    </script>
</body>

实例 - 图片切换


image.png
<body>
    <div id="app">
        <img :src="imgArr[index]" alt="">
        <a href="#" @click="prevImg" v-show="index != 0" class="left">prev</a>
        <a href="#" @click="nextImg" v-show="index != imgArr.length - 1" class="right">next</a>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                imgArr:[
                    "img/0.png",
                    "img/1.png",
                    "img/2.png",
                    "img/3.png"
                ],
                index:0
            },
            methods: {
                nextImg: function () {
                    ++this.index
                },
                prevImg: function () {
                    --this.index
                }
            }
        })
    </script>
</body>

v-for

<body>
    <div id="app">
        <input type="button" value="add" @click="add">
        <input type="button" value="remove" @click="remove">
        <ul>
            <li v-for="(item,index) in arr">{{ index + 1 }} city: {{ item }}</li>
        </ul>
        <h2 v-for="item in food" :title="item.name">{{ item.name }}</h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                arr:['beijing','shanghai','guangzhou','shenzhen'],
                food:[
                    {name: 'hot pot'},
                    {name: 'hot dry noodles'}
                ]
            },
            methods: {
                add:function () {
                    this.food.push({ name:'chao fan'})
                },
                remove:function () {
                    this.food.shift();
                }
            }
        })
    </script>
</body>

v-model

<body>
    <div id="app">
        <input type="text" v-model="message" @keyup.enter="getM">
        <input type="button" value="edit message" @click="setM">
        <h2>{{ message }}</h2>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:"#app",
            data:{
                message:'programmer'
            },
            methods: {
                getM:function () {
                    alert(this.message)
                },
                setM:function () {
                    this.message = 'dkw'
                }
            }
        })
    </script>
</body>
上一篇下一篇

猜你喜欢

热点阅读