【发布时间】:2019-06-13 18:06:56
【问题描述】:
我正在尝试在 chrome 扩展中运行一个示例 Vuejs 应用程序,作为一个新选项卡(即,当您打开一个新选项卡时,Vuejs 应用程序应该呈现)。当我使用simplehttpserver dist/ 从dist/ 文件夹运行应用程序时,它可以正常工作,但是,当我Load Unpacked 将 dist 文件夹作为 Chrome 扩展程序并尝试打开一个新选项卡时,Vuejs 应用程序不会呈现。
我正在运行npm run build 来构建应用程序,然后将manifest.json 和background.js 文件添加到dist/ 文件夹中。
我在 chrome 扩展版本的元素检查器中看到的一件事是 <div id="app"></div> 出于某种原因未包含在 DOM 中,即使它在构建完成后位于 index.html 文件中。
注意:我尝试用作 chrome 扩展的 Vuejs 应用是 vue init webpack . 创建的默认示例应用。
manifest.json
{
"manifest_version": 2,
"name": "Vuejs test extension",
"description": "",
"version": "0.0.0",
"chrome_url_overrides": {
"newtab": "index.html"
},
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs"
],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
background.js
chrome.browserAction.onClicked.addListener((function() {
chrome.tabs.create({'url': "chrome://newtab"})
}));
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Test vuejs app</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
【问题讨论】:
-
检查器中有什么,可能是一些文件丢失或没有加载?
-
是dist文件夹中的html吗?
-
虽然我没有使用带有扩展的 vue,但我记得读过 this article,关于渲染怪癖。这部分不幸的是,模板编译器依赖 eval 函数来执行这个编译任务,而这些在 CSP 下是不允许的。
-
您的 html 没有加载任何脚本。请注意,扩展页面是一个静态文件,它必须包含明确提到的所有资源,因为它不是由 node.js 等提供的。
-
另外,我在谷歌搜索问题标题时看到了很多答案,包括用作扩展基础的样板模板。
标签: javascript vue.js google-chrome-extension vuejs2