基本上参考一个 Vue公司 </跨度> 另一个实例 Vue公司 </跨度> 例如,如果我正确地理解了一切相应的JS代码分为两个文件,如下所示:
// componentOne.js新 Vue公司 </跨度> ( el :‘[data-init =“component-one”]’, 数据:{…}, 方法: {…});
// componentTwo.js新 Vue公司 </跨度> ( el:‘[data-init =“component-two”]’ 数据:{…} 方法: { doSomething:function(事件
使用vue-cli创建一个webpack入门应用程序。 vue init app --webpack
vue init app --webpack
然后,尝试以这种方式构建组件。阅读更多: https://vuejs.org/v2/guide/components.html#What-are-Components
import Vue from 'vue' import ComponentOne from './ComponentOne.vue' import ComponentTwo from './ComponentTwo.vue' new Vue({ el: '#app', template: '<App/>', components: { ComponentOne, ComponentTwo } })
<template> <div class="user"> <div v-for="user in users"> <p>Username: {{ user.username }}</p> </div> </div> </template> <script> export default { data () { return { users: [ {username: 'Bryan'}, {username: 'Gwen'}, {username: 'Gabriel'} ] } } } </script>
<template> <div class="two"> Hello World </div> </template> <script> export default { } </script>
组件二中的按钮元素在Vue中被称为插槽。 @click指令值的评估发生在父组件(组件一,主机组件二)中。因此,您需要在那里声明点击处理程序(在组件一上)。
如果你想在第二组内部处理处理程序,你应该在它的(组件二)模板中为slot元素声明一个click指令,并将处理程序函数作为一个pop传递。
祝好运。
<div th:if="${msg.replyFloor}"> <div class="msg-lists-item-left"> <span class="msg-left-edit" th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">����</span> <span th:text="${msg.topic.title}" class="msg-left-edit-res" th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">����ش�</span> <span th:text="${msg.type.name}" class="msg-left-edit " th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">�������</span> <span class="msg-left-edit-number" > ������<span th:text="${msg.unreadCount} ? : ${msg.unreadCount} + '����' : ${msg.unreadCount} + '��' " th:class="${msg.unreadCount} ? : 'number-inner':''">2132��</span>�ظ� </span> </div> <div class="msg-lists-item-right"> <span th:text="${msg.lastShowTime}">2017-8-10</span> </div> </div>
但是当我做一些内联的东西,比如{{3 + 3}}时,它会像它应该的那样计算。所以Vue知道有什么东西。
因为你有父实例'componentOne'。它为此模板激活了Vue。如果需要在内部设置另一个实例,则必须分离模板的一部分。示例(它可能滞后于片段!)。 替代
https://jsfiddle.net/qh8a8ebg/2/
// componentOne.js new Vue({ el: '[data-init="component-one"]', data: { text: 'first' }, methods: {} }); // componentTwo.js new Vue({ el: '[data-init="component-two"]', data: { text: 'second' }, template: `<button @click="doSomething($event)">{{text}}</button>`, methods: { doSomething: function(event) { console.log(event); } } });
<script src="https://vuejs.org/js/vue.min.js"></script> <div data-init="component-one"> {{text}} </div> <div data-init="component-two"> </div>