最近我正在使用 vuejs 开发 chrome 扩展,并且有相同的要求,需要将整个 Vue 应用程序注入到特定网页(即 google.com),但遗憾的是 vue-router 不起作用。
I used 这个样板 https://github.com/Kocal/vue-web-extension以最少的改变让它轻松工作。
使用此样板创建 Vue 项目后,您需要更新 src/popup/popup.js 以在网页中注入 vue 应用程序。
...
const div = document.createElement("div")
document.body.insertBefore(div, document.body.firstChild);
new Vue({
el: div,
render: h => { return h(App); }
});
在manifest.json中添加vue应用程序作为内容脚本,而不是browser_action或page_action。
"content_scripts": [{
"matches": ["*://*.google.com/*"],
"js": [
"popup/popup.js"
],
"css": ["/popup/popup.css"]
}]
这里 vue 应用程序将仅在 google.com 上注入。如果您想注入所有网页,请从 content_scripts 中删除匹配项
P.S.
管理 vue 路由器以模式工作abstract并打电话router.replace("/")
after new Vue({ ..., router })