zoukankan      html  css  js  c++  java
  • Vue.js 的v-for, v-html v-bind, v-show 实例

    <template>
        <div>
            <ComponentA v-for="(value,key) in fruitList" :key="key"></ComponentA>
            <ul>
                <li v-for="(item,index) in fruitList">{{index}}: {{item.name}} : {{item.price}}</li>
            </ul>
            <ul>
                <li v-for="(index, key,value) in fruitList">{{index}}: {{key}} : {{value}}</li>
            </ul>
            <p v-for="item in fruitList"></p>
            <p v-html="withHtml">
                {{hello}}<br>
                this is from app2.vue, maybe will import by some js;
            </p>
            <button @click="addItem">按我</button>
            <button @click="changeItem">按我+1</button>
            <a :href="link">动态绑定</a>
            <a :class="classBind">动态绑定通常用于绑定class</a>
            <a class="link" :class="classArrayBind">动态绑定也可在绑定class的时候使用数组或者对象</a>
            <a :class="hasError?classArrayBind[0]:classArrayBind[1]">动态绑定的条件判断</a>
            <a :style="styleExample">绑定样式表</a>
            <button @click="changeColor">换颜色</button>
            <button @click="changeShow">看见?</button>
            <a v-show="ifShow">你看见我了</a>
        </div>
    </template>
    
    <script>
        import Vue from 'vue'
        import ComponentA from './components/a'
        export default {
            //渲染一个子component
            components: {
                ComponentA: ComponentA
            },
            data() {
                return {
                    classArrayBind: [{
                            classA: 'red'
                        },
                        {
                            classB: 'green'
                        }
                    ],
                    styleExample: {
                        'font-size': '20px',
                        'color': 'red'
                    },
                    hasError: false,
                    ifShow: true,
                    classBind: 'redFont',
                    link: 'http://www.baidu.com',
                    hello: 'world',
                    word: 'this is from app2.vue, maybe will import by some js;',
                    withHtml: '<i>this is about i</i>',
                    //渲染数组
                    fruitList: [{
                            name: 'apple',
                            price: 34
                        },
                        {
                            name: 'banana',
                            price: 56
                        },
                    ],
                    //渲染对象
                    objList: {
                        name: 'apple',
                        price: 345,
    
                    }
                };
            },
            methods: {
                addItem: function() {
                    //console.info(this.fruitList)
                    this.fruitList.push({
                        name: 'peach',
                        price: 30
                    })
                },
                changeItem: function() {
                    Vue.set(this.fruitList, 1, {
                        name: 'pineapple',
                        price: 999
                    })
                    this.fruitList[0].price = this.fruitList[0].price + 1
                },
                changeColor: function() {
                    this.styleExample.color = 'green'
                },
                changeShow: function() {
                    this.ifShow = !this.ifShow
                }
            }
        }
    </script>
    
    <style>
    
    </style>

    以上是app.vue的内容

    v-bind多半用于标签<a>的属性绑定, v-model用于绑定输入框.

    应该是这样.

  • 相关阅读:
    PHP 5.5.0 Alpha5 发布
    Ubuntu Touch 只是另一个 Android 皮肤?
    MariaDB 10 已经为动态列提供文档说明
    Percona Toolkit 2.1.9 发布,MySQL 管理工具
    Oracle Linux 6.4 发布
    Ruby 2.0.0 首个稳定版本(p0)发布
    Apache Pig 0.11.0 发布,大规模数据分析
    Node.js 0.8.21 稳定版发布
    红薯 MySQL 5.5 和 5.6 默认参数值的差异
    Django 1.5 正式版发布,支持 Python 3
  • 原文地址:https://www.cnblogs.com/Montauk/p/10062529.html
Copyright © 2011-2022 走看看