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用于绑定输入框.

    应该是这样.

  • 相关阅读:
    iBatis查询时报“列名无效”无列名的错误原因及解决方法
    【转】Spring结合Quartz实现多任务定时调用
    关于jar中读取包内和包外文件
    【摘自百度文库】数据库水平切分的实现原理解析
    web.xml 中的listener、 filter、servlet 加载顺序及其详解(转载)
    EL表达式
    【转】使用XFire+Spring构建Web Service
    慎用href="javascript:void(0)"
    POI导出EXCEL【摘自:oschina.net】
    【转】IBM websphere6.1 不支持泛型、intInteger类型的自动装箱和拆箱问题
  • 原文地址:https://www.cnblogs.com/Montauk/p/10062529.html
Copyright © 2011-2022 走看看