{"id":239232,"date":"2022-04-13T10:01:06","date_gmt":"2022-04-13T02:01:06","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239232"},"modified":"2022-03-30T10:01:55","modified_gmt":"2022-03-30T02:01:55","slug":"vue-mixin-js","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vue-mixin-js.html","title":{"rendered":"Vue\u7ec4\u4ef6\u516c\u7528\u65b9\u6cd5\u5982\u4f55\u63d0\u53d6mixin"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Vue\u7ec4\u4ef6\u516c\u7528\u65b9\u6cd5\u63d0\u53d6mixin\u5b9e\u73b0\uff0c\u591a\u4e2a\u7ec4\u4ef6\u5171\u7528\u4e00\u4e2a\u65b9\u6cd5\u65f6\u53ef\u4ee5\u7528 mixin \u62bd\u53d6\u5230\u4e00\u4e2ajs\u6587\u4ef6\u4e2d\uff0c\u4f5c\u4e3a\u5171\u7528\u65b9\u6cd5\uff0c\u4e0b\u9762\u4e00\u8d77\u8fdb\u5165\u6587\u7ae0\u4e86\u89e3\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00.\u5e94\u7528\u573a\u666f<\/strong><\/div>\n

\u591a\u4e2a\u7ec4\u4ef6\u5171\u7528\u4e00\u4e2a\u65b9\u6cd5\u65f6\u53ef\u4ee5\u7528 mixin \u62bd\u53d6\u5230\u4e00\u4e2ajs\u6587\u4ef6\u4e2d\uff0c\u4f5c\u4e3a\u5171\u7528\u65b9\u6cd5<\/p>\n

\u4e8c.\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/div>\n
1.\u63d0\u53d6js\u5171\u7528\u65b9\u6cd5\u6587\u4ef6<\/strong><\/span><\/div>\n

\"\"<\/p>\n

\r\nexport const common = {\r\n     \r\n    \/\/ \u7ec4\u4ef6\u5171\u7528\u5c5e\u6027 ----------------------------------\r\n    data() {\r\n        return {\r\n            age: 18                    \/\/ \u8bbe\u7f6e\u4e00\u4e2a\u5171\u7528\u5c5e\u6027\r\n        }\r\n    },\r\n    \/\/ ---------------------------------------------\r\n     \r\n    \/\/ \u7ec4\u4ef6\u5171\u7528\u65b9\u6cd5 ---------------------------------------------\r\n    methods: {\r\n        showName() {\r\n            alert(this.name)          \/\/ \u5f39\u51fa\u7ec4\u4ef6\u4e2dname\u5c5e\u6027\u7684\u5171\u7528\u65b9\u6cd5\r\n        }\r\n    },\r\n    \/\/ --------------------------------------------------------\r\n     \r\n    \/\/ \u7ec4\u4ef6\u6302\u8f7d\u65f6\u7684\u5171\u7528\u65b9\u6cd5 ----------------------\r\n    mounted() {\r\n        console.log('\u521d\u59cb\u5316\u65b9\u6cd5')    \/\/ \u6302\u8f7d\u65f6\u8c03\u7528\r\n    },\r\n    \/\/ ----------------------------------------\r\n     \r\n}\r\n<\/pre>\n
2.\u5f15\u5165<\/strong><\/span><\/div>\n
\r\n< template>\r\n    < div>\r\n        < div>\u7ec4\u4ef6< \/div>\r\n        < button @click=\"showName\">\u5f39\u51fa\u59d3\u540d< \/button>\r\n    < \/div>\r\n< \/template>\r\n \r\n< script>\r\n \r\n    \/\/ \u5f15\u5165js\u6587\u4ef6\u4e2d\u7684\u65b9\u6cd5\u5bf9\u8c61 --------------------\r\n    import {common, } from '..\/pub_js\/common.js'\r\n    \/\/ ----------------------------------------\r\n     \r\n    export default {\r\n        name: 'Student',\r\n        data() {\r\n            return {\r\n                name: '\u5f20\u4e09'\r\n            }\r\n        },\r\n         \r\n        \/\/ \u8c03\u7528 mixin \u5c06\u7ec4\u4ef6js\u4e0e\u5171\u7528js\u5408\u5e76 ---\r\n        mixins: [common, ],\r\n        \/\/ --------------------------------\r\n         \r\n    }\r\n< \/script>\r\n<\/pre>\n
\u4e09.\u6ce8\u610f\u4e8b\u9879<\/strong><\/div>\n

1.data\u4e2d\u7684\u5c5e\u6027\u5408\u5e76\u540e\uff0c\u5982\u679c\u6709\u547d\u540d\u51b2\u7a81\u7684\u60c5\u51b5\uff0c\u4ee5\u7ec4\u4ef6\u4e2d\u7684\u5c5e\u6027\u4e3a\u4e3b\uff0c\u3010\u7ec4\u4ef6\u5c5e\u6027\u8986\u76d6\u5171\u7528js\u4e2d\u7684\u5c5e\u6027\u3011<\/p>\n

2.methods\u4e2d\u7684\u65b9\u6cd5\u5408\u5e76\u540e\uff0c\u5982\u679c\u6709\u547d\u540d\u51b2\u7a81\u7684\u60c5\u51b5\uff0c\u4ee5\u7ec4\u4ef6\u4e2d\u7684\u65b9\u6cd5\u4e3a\u4e3b\uff0c\u3010\u7ec4\u4ef6\u65b9\u6cd5\u8986\u76d6\u5171\u7528js\u4e2d\u7684\u65b9\u6cd5\u3011<\/p>\n

3.mounted\u7b49\u751f\u547d\u5468\u671f\u65b9\u6cd5\u5408\u5e76\u540e\uff0c\u4f1a\u5148\u8c03\u7528\u5171\u7528js\u4e2d\u7684\u7684\u751f\u547d\u5468\u671f\u51fd\u6570\uff0c\u518d\u8c03\u7528\u7ec4\u4ef6\u4e2d\u7684\u751f\u547d\u5468\u671f\u51fd\u6570\uff0c\u3010\u4e0d\u4f1a\u8fdb\u884c\u8986\u76d6\u3011<\/p>\n

4.\u5982\u679c\u662f\u5168\u5c40\u5171\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u6302\u8f7d\u5230main.js\u4e2d\u2193<\/p>\n

\r\nimport Vue from 'vue'\r\nimport App from '.\/App.vue'\r\n \r\nVue.config.productionTip = false\r\n \r\n\/\/ \u6302\u8f7d\u5168\u5c40\u5171\u7528\u65b9\u6cd5 -----------------------\r\nimport {common, } from 'pub_js\/common.js'\r\nVue.mixin(common)\r\n\/\/ --------------------------------------\r\n \r\nnew Vue({\r\n  render: h => h(App),\r\n}).$mount('#app')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u591a\u4e2a\u7ec4\u4ef6\u5171\u7528\u4e00\u4e2a\u65b9\u6cd5\u65f6\u53ef\u4ee5\u7528 mixin \u62bd\u53d6\u5230\u4e00\u4e2ajs\u6587\u4ef6\u4e2d\uff0c\u4f5c\u4e3a\u5171\u7528\u65b9\u6cd5 export const com […]<\/p>\n","protected":false},"author":1479,"featured_media":239240,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239232"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239232\/revisions"}],"predecessor-version":[{"id":239242,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239232\/revisions\/239242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/239240"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}