美文网首页
vue单组件应用

vue单组件应用

作者: 琳媚儿 | 来源:发表于2020-05-16 20:07 被阅读0次
   <input type="text" placeholder="水果名" ref="fruitname" />
        <input type="number" placeholder="价格" min="0" max="100" ref="fruitprice">
        <button @click="addData">添加数据</button>
    
        <ul v-for="(product,index) in products" :key="index">
            <li>
                {{product.name}}
                <input type="number" v-model.number="product.quantity" min="0" max="100">
                <span v-if="product.quantity===0">
                    _OUT OF STOCK
                </span>
                <!-- <button @click="product.quantity +=1">ADD</button> -->
                <button @click="deletebtn(index)">Delete</button>
            </li>
            <!-- <li>{{product.addname}</li> -->
        </ul>
        <h2>Total Inventory :{{totalProducts}}</h2>
<script>
        new Vue({
            el: '#app',
            data: {
                products: []
            },
//计算属性:求总价格
            computed: {
                totalProducts() {
                    return this.products.reduce((sum, product) => {
                        return sum + product.quantity
                    }, 0)
                }s
            },

            mounted(e) {
                //http://www.cjlly.com:3008/page
                fetch('http://www.cjlly.com:3008/page')
                    .then(response => response.json())
                    .then(json => {
                        this.products = json
                    })
            },
            methods: {
                addData: function (products) {
                    //获得输入框数据
                    let fruitname = this.$refs.fruitname.value;  //获得输入框数据
                    let fruitprice = this.$refs.fruitprice.value;
                    this.products.push({         //将输入框中的内容push 到数组中
                        name: fruitname,
                        quantity: fruitprice

                    })
                    console.log(fruitname, fruitprice);
                },
//根据引索,进行删除
                deletebtn: function (index) {
                    this.products.splice(index, 1);
                }
            }
        })
    </script>

访问子组件实例或子元素

尽管存在 prop 和事件,有的时候你仍可能需要在 JavaScript 里直接访问一个子组件。为了达到这个目的,你可以通过 ref这个 attribute 为子组件赋予一个 ID 引用。例如:

<input ref="usernameInput"></input>

现在在你已经定义了这个 ref 的组件里,你可以使用:获得输入框中的内容

this.$refs.usernameInput.value

实例方法 / 事件

只配合一个事件名使用 $emit

Vue.component('welcome-button', {
  template: `
    <button v-on:click="$emit('welcome')">
      Click me to be welcomed
    </button>
  `
})
<div id="emit-example-simple">
  <welcome-button v-on:welcome="sayHi"></welcome-button>
</div>
new Vue({
  el: '#emit-example-simple',
  methods: {
    sayHi: function () {
      alert('Hi!')
    }
  }
})
Screenshot.png

相关文章

  • cnode社区

    面试要点:(单页应用重点在vue-router) 单页应用,页面只有一个 APP组件; vue-router实现页...

  • 创建单页应用

    Vue.js + Vue Router 创建单页应用, 使用vuex还管理组件间的通信。 State:存放组件之间...

  • Vue组件通信大全(终结篇)

    背景   Vue是单页面应用,单页面应用又是由组件构成,各个组件之间又互相关联,那么如何实现组件之间通信就显得尤为...

  • vue单组件应用

    访问子组件实例或子元素 尽管存在 prop 和事件,有的时候你仍可能需要在 JavaScript 里直接访问一个子...

  • 浅析vue-router中name的用法

    我们常用vue.js和vue-router来创建单页应用,vue-router能很方便的管理所有的单页组件。我们在...

  • vue性能优化

    Vue 应用运行时性能优化措施 引入生产环境的 Vue 文件 使用单文件组件预编译模板 提取组件的 CSS 到单独...

  • swiper4.5.1在Vue中的应用

    swiper4在vue的应用 swiper4安装 swiper4在单文件组件中的使用 在单文件组件

  • vue-cli

    === 单文件组件结合webpack处理单文件组件配置webpack相关loader使用vue文件创建vue组件引...

  • webpack优化技巧总结

    在vue应用程序中使用 单文件组件 优化Vue构建 浏览器缓存管理 代码分割(code splitting) 关于...

  • vue学习之vue-router使用整理笔记

    用Vue.js + vue-router创建单页应用,是非常简单的,基本是这样的: 组件 → 路由 → 渲染地方 ...

网友评论

      本文标题:vue单组件应用

      本文链接:https://www.haomeiwen.com/subject/jdgrohtx.html