【问题标题】:use VueRouter and Vue.js in Chrome Extension - Issues with path segments在 Chrome 扩展中使用 VueRouter 和 Vue.js - 路径段问题
【发布时间】:2017-06-13 19:45:02
【问题描述】:

我用 Vue.js 开发了一个 chrome 扩展。在我想使用路由时工作正常。

在我拥有 localhost:8080/ 的本地开发服务器上,使用以下路由设置时这不是问题:

ma​​in.js

import Vue from "vue";
import VueRouter from "vue-router";
import App from "./App.vue";
import OptionComponent from "./OptionComponent.vue";

const routes = [
  { path: '/', component: App },
  { path: '/option', component: OptionComponent },
];

Vue.use(VueRouter); // This makes all the magic hapen and Vue recognizes the router-view and router-link

const router = new VueRouter({
  routes,
});

new Vue({
  el: '#app',
  router,
});

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CM Server Descriptor</title>
  </head>
  <body>
    <div id="app">
      <router-view></router-view>
    </div>
    <script src="libs/crypt.js"></script>
    <script src="libs/jquery.js"></script>
    <script src="libs/aja.min.js"></script>
    <script src="libs/JSLINQ.js"></script>
    <script src="js/build.js"></script>
  </body>
</html>

当我部署到我的 Chrome 扩展程序并开始测试它时,没有任何反应。 我发现 chrome 扩展有一些特殊的路径行为。

在这里你可以看到 chrome 的路径是 /index.html ,这是额外的。

然后我尝试了以下内容:

const routes = [
  {path: '/' + chrome.runtime.id + '/index.html', component: App},
  {path: '/' + chrome.runtime.id + '/option', component: OptionComponent},
];

没有帮助。更改为 /index/ 也没有帮助...... 最后一次尝试是试图明确地告诉协议:

  {path: 'chrome-extension://' + chrome.runtime.id + '/', component: App},

也没有运气。 我假设 VueRouter 只作用于 http:// 协议网址。

如果有人知道如何解决这个问题,我将非常感激!

【问题讨论】:

  • 我不知道 vue.js 但我知道 Chrome 并且它没有内置的 Web 服务器,因此您需要在扩展程序中使用相对于 manifest.json 文件夹的真实 html 文件路径喜欢path: '/index.html'
  • 遗憾的是没有奏效。我也试过{ path: 'index.html/', component: App },,但没有成功
  • 对不起,我的意思是输入 /index.html - 是一个错字。是的 index.html 与清单位于同一根目录中。我将 index.html 添加到问题中

标签: javascript google-chrome-extension routing vue.js vue-router


【解决方案1】:

我遇到了同样的问题,我终于解决了。已经一年了,所以不确定它是由 chrome 还是 Vue 修复的。

不管怎样,我会为刚来这里的人写下:

Chrome 基于文件夹结构传递 URL,没有隐式 URL 解析。这意味着/ 不会重定向到index.html。所以解决办法是:

  • index.html添加路径:

    { path: '/index.html', component: App },
    
  • 或者为您的应用添加路径并在加载时手动推送。

    //router.js
    { path: '/app', component: App },
    

文件App.vue

created(){
    this.$router.push('app');
}

请记住,路由路径需要与您的 chrome 扩展根目录中的相对路径完全匹配。所以如果你把index.html放在扩展根目录下,你的VuebaseUrl必须是/

【讨论】:

    【解决方案2】:

    Chrome 指向您在 mainfest 中注册的 popup.html 文件,然后您的路由器将找不到该文件。

    将基本 url 设置为 popup.html(取决于它相对于 mainfest.json 的位置)解决了它

    const router = new VueRouter({
      base: '/popup/popup.html',
      mode: 'history',
      routes,
    });
    

    【讨论】:

    • 就是这样。查看我项目中的 Dist 文件夹,它没有嵌套在弹出目录中。所以我把基地改成了/popup.html
    • 非常感谢,这正是我需要的答案:D
    【解决方案3】:

    Chrome 扩展程序必须遵守 CSP(内容安全策略),因此您不能使用 Vue 2,因为它基于 eval()。

    需要使用CSP版本的Vue和Vue-router 1.0版本来搭配:

    Vue 1.0-csp

    https://github.com/vuejs/vue/tree/1.0-csp

    Vue-router 1.0

    https://github.com/vuejs/vue-router/tree/1.0

    【讨论】:

    猜你喜欢
    • 2016-04-09
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多