{"id":986,"date":"2023-03-23T22:41:43","date_gmt":"2023-03-23T14:41:43","guid":{"rendered":""},"modified":"2023-03-23T22:41:43","modified_gmt":"2023-03-23T14:41:43","slug":"Vue.js Mixins","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/986.html","title":{"rendered":"Vue.js Mixins"},"content":{"rendered":"
\n
js mixins\u786e\u4fdd\u4e0d\u9700\u8981\u91cd\u590d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5206\u914dVue.js\u7ec4\u4ef6\u7684\u53ef\u91cd\u7528\u529f\u80fd\uff0c\u5e76\u4e00\u6b21\u53c8\u4e00\u6b21\u5730\u4f7f\u7528\u5b83\u4eec\u3002<\/span> <\/body>
\n js mixins\u63d0\u4f9b\u4e86\u51fa\u8272\u7684\u7075\u6d3b\u6027\uff0cmixin\u5bf9\u8c61\u5305\u542bVue\u7ec4\u4ef6\u9009\u9879\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u662fVue.js\u7684mixin\u548c\u7ec4\u4ef6\u9009\u9879\u7684\u7ec4\u5408\u3002<\/span>
\n js mixins\u63d0\u4f9b\u4e86\u51fa\u8272\u7684\u5b89\u5168\u529f\u80fd\uff0c\u5982\u679c\u7f16\u5199\u5f97\u5f53\uff0c\u5b83\u4eec\u4e0d\u4f1a\u5f71\u54cd\u8d85\u51fa\u5b9a\u4e49\u8303\u56f4\u7684\u66f4\u6539\u3002<\/span>
\n js mixins\u662f\u5b9e\u73b0\u4ee3\u7801\u53ef\u91cd\u7528\u6027\u7684\u7edd\u4f73\u5e73\u53f0\u3002<\/span> <\/p>\n Vue.js mixins\u5e94\u8be5\u89e3\u51b3\u7684\u95ee\u9898<\/h3>\n
<template>
<div><\/span>
<button v-on:click=\"clicked('you just clicked on button 1')\">
Button 1
<\/button><\/span>
<\/div><\/span>
<\/template>;
export default {
name: \"Test\",
methods: {
clicked(value) {
alert(value);
}
}
};
<\/span><\/code><\/pre>\n<\/p><\/div>\n <template>
<div><\/span>
<button v-on:click=\"clicked('you just clicked on button 2')\">
Button 2
<\/button><\/span>
<\/div><\/span>
<\/template>;
export default {
name: \"Modal\",
methods: {
clicked(value) {
alert(value);
}
}
};
<\/span><\/code><\/pre>\n<\/p><\/div>\n <template>
<div id=\"app\">
<img alt=\"Vue logo\" src=\".\/assets\/logo.png\">
<Test \/>
<modal \/>
<\/div><\/span>
<\/template>;
<script><\/span>
import Test from \".\/components\/Test.vue\";
import Modal from \".\/components\/Modal.vue\";
export default {
name: \"app\",
components: {
Test,
Modal
}
};
<\/script><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n Vue.js Mixin\u8bed\u6cd5<\/h3>\n
\/\/ define a mixin object
var myMixin = {
created: function() {
this.hello();
},
methods: {
hello: function() {
console.log(\"hello from mixin!\");
}
}
};
\/\/ define a component that uses this mixin
var Component = Vue.extend({
mixins: [myMixin]
});
var component = new Component(); \/\/ => \"hello from mixin!\"
<\/span><\/code><\/pre>\n<\/p><\/div>\n <html><\/span>
<head><\/span>
<title><\/span>Vue.js <\/title><\/span>
<link rel=\"stylesheet\" href=\"index.css\">
<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<\/head><\/span>
<body><\/span>
<div id = \"mixin_1\"><\/div><\/span>
<script type = \"text\/javascript\">
var vm = new Vue({
el: '#mixin_1',
data: {
},
methods : {
},
});
var myMixin = {
created: function () {
this.startmixin()
},
methods: {
startmixin: function () {
alert(\"This is a Vue.js Mixin example!!\");
}
}
};
<\/script><\/span>
<script src=\"index.js\"><\/script><\/span>
<\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n var Component = Vue.extend({
mixins: [myMixin]
})
var component = new Component()
<\/span><\/code><\/pre>\n<\/p><\/div>\n html, body {
margin: 5px;
padding: 0;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/div>\n
\n Python \u53d8\u91cf >> \n <\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Vue.js Mixinszh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[122],"tags":[],"class_list":["post-986","post","type-post","status-publish","format-standard","hentry","category-vuejc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/986"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=986"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/986\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}