您好,登錄后才能下訂單哦!
這篇文章主要介紹了vue2中組件怎么互相調(diào)用的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇vue2中組件怎么互相調(diào)用文章都會有所收獲,下面我們一起來看看吧。
我打算用全局mixin來做這個功能。本來打算在每個組件里面定義name
來綁定methods的,考慮到這樣做每個vue組件里面都要自己手動定義name
,而且也容易存在重名的情況,于是我就打算用vue組件所在的路徑來做,我發(fā)現(xiàn)vue組件實例上$options的prototype下有個__file屬性記錄了當前文件的路徑,當時生產(chǎn)環(huán)境下就沒有了,于是我想到了寫個weboack插件來實現(xiàn),另外吐槽下webpack的鉤子真的多,示例不清晰。vue2項目大多數(shù)都是使用的js,代碼提示用jsconfig.json結(jié)合types, js代碼里面用注釋jsdoc語法添加代碼提示。
直接在組件里面調(diào)用globalDispatch方法,有代碼提示的哦,考慮到一個組件可能同時調(diào)用了多次,所有可以多傳一個eKey 進行精確emit。在組件上可以進行eKey綁定(也可以寫e-key)。
import Vue from "vue"; const DEFAULT_E_KEY = "__default"; /** * 方法合集 * @type {Record<string, {eKey: string; handler: function}[]>} */ const events = {}; /** * 全局調(diào)用event的mixin * @type {Vue & import("vue").ComponentOptions} */ const globalDispatch = { created() { const attrs = this.$attrs; const eKey = attrs.eKey ?? attrs["e-key"]; const filePath = this.$options.__file ?? this.$options.__filePath; filePath && addEvents(filePath, this, eKey); }, destroyed() { const filePath = this.$options.__file ?? this.$options.__filePath; filePath && removeEvents(filePath, this); } }; /** * 監(jiān)聽方法 * @param {string} filePath 獲取到的路徑 * @param {Vue} vm vue組件實例 * @param {string=} eKey event key */ function addEvents(filePath, vm, eKey = DEFAULT_E_KEY) { const methods = vm.$options.methods; if (methods) { Object.entries(methods).forEach(([key, handler]) => { handler = handler.bind(vm); handler.vm = vm; const eventKey = `${filePath}:${key}`; const event = { eKey, handler }; if (events[eventKey] && events[eventKey].length) { events[eventKey].push(event); } else { events[eventKey] = [event]; } }); } } /** * 移除方法 * @param {string} filePath 獲取到的路徑 * @param {Vue} vm vue組件實例 */ function removeEvents(filePath, vm) { Object.keys(events).forEach(key => { if (key.startsWith(filePath)) { events[key] = events[key].filter(v => v.handler.vm !== vm); } }); } /** * * @param {import("../../types/event-keys").EventKeys | import("../../types/shims-vue").EventParams} params * @param {...any} args * @returns */ Vue.prototype.globalDispatch = function dispatch(params, ...args) { let eventKey, eKey = DEFAULT_E_KEY; if (typeof params === "string") { eventKey = params; } else if (typeof params === "object") { eventKey = params.target; eKey = params.eKey ?? DEFAULT_E_KEY; } const eKeyMsg = eKey !== DEFAULT_E_KEY ? `eKey:${eKey},` : ""; if ( !eventKey || typeof eventKey !== "string" || !/^[^:]*:[^:](.*){1}$/.test(eventKey) ) { throw new Error(`${eKeyMsg}eventKey:${eventKey}, 參數(shù)不正確!`); } const handlers = events[eventKey]?.filter(v => v.eKey === eKey); if (handlers && handlers.length) { const results = handlers.map(v => v.handler(...args)); if (results.length === 1) return results[0]; return results.map(result => ({ eKey, result })); } const method = eventKey.split(":")[1]; throw new Error(`${eKeyMsg}method:${method},該方法未找到!`); }; export default globalDispatch;
這個文件主要添加所有的組件的methods到events里面,在Vue.prototype上掛載globalDispatch 方法,方便在vue組件上使用。
我用的時vue2.7版本寫的,主要時include把types文件夾的文件加進來
{ "compilerOptions": { "moduleResolution": "node", "target": "esnext", "baseUrl": ".", "allowJs": true, "sourceMap": false, "strict": true, "jsx": "preserve", "module": "ESNext", "paths": { "@/*": ["./src/*"] }, "lib": ["DOM", "ESNext"] }, "vueCompilerOptions": { "target": 2.7 }, "exclude": ["node_modules", "dist"], "include": ["src/**/*.js", "src/**/*.vue", "types/**/*.ts", "types/**/*.d.ts"] }
在types文件夾下新建shims-vue.d.ts, 因為globalDispatch需要支持兩種傳參形式,所以使用重載
import Vue from "vue"; import { EventKeys } from "./event-keys"; export type EventParams = { target: EventKeys; eKey: string }; function globalDispatch(eventKey: EventKeys, ...args: any[]): any; function globalDispatch(eventParams: EventParams, ...args: any[]): any; declare module "vue/types/vue" { interface Vue { /** * 全局互相調(diào)用event的dispatch */ globalDispatch: typeof globalDispatch; } }
在types文件夾下新建event-keys.d.ts, 這個文件是用來給globalDispatch的第一個參數(shù)做代碼提示的,手動寫可以,寫個webpack插件自動讀取vue文件的路徑和方法自動生成更好,下面會貼出來。
export type EventKeys = "src/App.vue:onClick" | "src/views/IndexView.vue:test";
在項目根目錄下新建plugins文件夾
開發(fā)者模式下才需要生成event-keys.d.ts,先遞歸找出所有的vue文件的路徑,然后讀取文件,用acorn庫解析,找出文件的methods里的所有方法名,用prettier格式化后寫入到event-keys.d.ts,在項目啟動和文件變化后都會執(zhí)行,在添加methos里新方法或刪除后,會執(zhí)行寫入。
const fs = require("fs"); const path = require("path"); const acorn = require("acorn"); const prettier = require("prettier"); const prettierConfig = require("../prettier.config"); /** * @typedef {import("webpack/lib/Compiler")} Compiler */ const PLUGIN_NAME = "global-dispatch"; const KEYS_PATH = path.resolve(__dirname, "../types/event-keys.d.ts"); class TransformFilePathPlugin { /** * @param {Compiler} compiler * @returns {void} */ apply(compiler) { compiler.hooks.done.tap(PLUGIN_NAME, () => { process.env.NODE_ENV === "development" && writeEventKeys(); }); } } function writeEventKeys() { const vueFilePaths = getFilePath(); writeVueKeyPaths(vueFilePaths); } /** * 緩存內(nèi)容,防止重復(fù)寫入 */ let keysContentCache = fs.readFileSync(KEYS_PATH, "utf-8"); /** * 寫入__filePath到type Key文件 * @param {string[]} paths 路徑集合 */ function writeVueKeyPaths(paths) { let keysContent = "export type EventKeys ="; const keys = []; paths.forEach(p => { let content = fs.readFileSync(getSrcPath(p), "utf-8"); const scriptMatch = content.match(/<script/g); if (!scriptMatch) return; const startIndex = content.indexOf("export default"); if (startIndex < 0) return; const endIndex = content.indexOf("</script>", startIndex); content = content.substring(startIndex, endIndex); const ast = acorn.parse(content, { sourceType: "module" }); const defaultExportAst = ast.body.find( v => v.type === "ExportDefaultDeclaration" ); let properties; if (defaultExportAst.declaration.type === "CallExpression") { properties = defaultExportAst.declaration.arguments[0].properties; } if ( defaultExportAst.declaration.type === "ObjectExpression" && Array.isArray(defaultExportAst.declaration.properties) ) { properties = defaultExportAst.declaration.properties; } const methods = properties.find(v => v.key.name === "methods"); if (!methods) return; if (methods.value.properties.length) { const methodNames = methods.value.properties.map( v => `${p}:${v.key.name}` ); keys.push(...methodNames); } }); keysContent += keys.map(v => `'${v}'`).join("|") || "string"; keysContent = prettier.format(keysContent, { ...prettierConfig, parser: "typescript" }); if (keysContentCache !== keysContent) { keysContentCache = keysContent; fs.writeFileSync(KEYS_PATH, keysContent); } } /** * * @param {string=} p 路徑 * @returns {string[]} 路徑集合 */ function getFilePath(p = "src") { const paths = fs.readdirSync(getSrcPath(p), "utf-8"); const vueFiles = getVueFiles(paths, p); const dirs = getDirs(paths, p); if (dirs.length) { dirs.forEach(dir => { vueFiles.push(...getFilePath(dir)); }); } return vueFiles; } function getDirs(paths, path) { return paths .map(v => `${path}/${v}`) .filter(v => fs.statSync(v).isDirectory()); } function getVueFiles(paths, path) { return paths.filter(v => v.endsWith(".vue")).map(v => `${path}/${v}`); } function getSrcPath(p) { return path.resolve(__dirname, "../" + p); } module.exports = { TransformFilePathPlugin };
這個文件是用來在vue實例上添加__filePath屬性的,本來是想寫在上面的插件一起的,無奈沒有在webpack文檔等地方找到在plugins里添加loader的方法,在vue-loader源碼里也沒有好的體現(xiàn)。 在開發(fā)者環(huán)境下vue的$options下有__file可以用,所以只需要生產(chǎn)環(huán)境啟用
module.exports = function(content) { if (process.env.NODE_ENV === "development") return content; const filePath = this.resourcePath .replace(/\\/g, "/") .replace(/(.*)?src/, "src"); const reg = /export default.*?{/; content = content.replace(reg, $0 => `${$0} __filePath: "${filePath}",`); return content; };
添加configureWebpack里的即可
const path = require("path"); const { TransformFilePathPlugin } = require("./plugins/global-dispatch"); /** * @type {import('@vue/cli-service').ProjectOptions} */ module.exports = { lintOnSave: false, productionSourceMap: false, configureWebpack: { plugins: [new TransformFilePathPlugin()], module: { rules: [ { test: /\.vue$/, use: [ { loader: path.resolve(__dirname, "./plugins/vue-path-loader.js") } ] } ] } } };
關(guān)于“vue2中組件怎么互相調(diào)用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“vue2中組件怎么互相調(diào)用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。