Vue


mixin 混入

<pre><code class="language-javascript">Student 组件 &amp;lt;template&amp;gt; &amp;lt;div&amp;gt; &amp;lt;h2 @click=&amp;quot;showname&amp;quot;&amp;gt;姓名:{{ name }}&amp;lt;/h2&amp;gt; &amp;lt;h2&amp;gt;性别:{{ sex }}&amp;lt;/h2&amp;gt; &amp;lt;h2&amp;gt;{{ x }}和{{ y }}&amp;lt;/h2&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/template&amp;gt; &amp;lt;script&amp;gt; //引入混合 import { hunhe, hunhe2 } from &amp;quot;../mixin&amp;quot;; export default { name: &amp;quot;Student&amp;quot;, data() { return { name: &amp;quot;Andy&amp;quot;, sex: &amp;quot;male&amp;quot;, }; }, mounted() { console.log(&amp;quot;我是student的mounted&amp;quot;); }, mixins: [hunhe, hunhe2], }; &amp;lt;/script&amp;gt;</code></pre> <pre><code class="language-javascript">School 组件 &amp;lt;template&amp;gt; &amp;lt;div&amp;gt; &amp;lt;h2 @click=&amp;quot;showname&amp;quot;&amp;gt;名称:{{ name }}&amp;lt;/h2&amp;gt; &amp;lt;h2&amp;gt;地点:{{ address }}&amp;lt;/h2&amp;gt; &amp;lt;h2&amp;gt;{{ x }}和{{ y }}&amp;lt;/h2&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/template&amp;gt; &amp;lt;script&amp;gt; //引入混合 import { hunhe, hunhe2 } from &amp;quot;../mixin&amp;quot;; export default { name: &amp;quot;School&amp;quot;, data() { return { name: &amp;quot;TKKC&amp;quot;, address: &amp;quot;Xiamen&amp;quot;, //混合的优先级低一些 x: 666, }; }, mounted() { console.log(&amp;quot;我是school的mounted&amp;quot;); }, mixins: [hunhe, hunhe2], }; &amp;lt;/script&amp;gt;</code></pre> <pre><code class="language-javascript">mixin.js export const hunhe = { methods: { showname() { alert(this.name) } }, mounted() { console.log('我是mixin') } } export const hunhe2 = { data() { return { x:100, y:200 } }, }</code></pre> <pre><code class="language-javascript">App 组件 &amp;lt;template&amp;gt; &amp;lt;div&amp;gt; &amp;lt;Student&amp;gt;&amp;lt;/Student&amp;gt; &amp;lt;hr&amp;gt; &amp;lt;School&amp;gt;&amp;lt;/School&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/template&amp;gt; &amp;lt;script&amp;gt; import Student from './components/Student.vue' import School from './components/School.vue' export default { name:'App', components:{Student,School} } &amp;lt;/script&amp;gt;</code></pre> <p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/f6c9a50733c9b62ea733891b3c47c6d0" alt="" /></p>

页面列表

ITEM_HTML