在ES6中导入包:“无法解析模块说明符“ vue””

zer*_*dge 10 javascript vue.js

尝试按照一些Vue教程进行操作,目前我无法将Vue导入.js文件中,然后将该文件导入到我的index.html。这就是我在我的脚本中导入脚本的方式index.html

<script src="./js/main.js" type="module"></script>
Run Code Online (Sandbox Code Playgroud)

如果我在main.js文件中这样做:

import Vue from 'vue';
Run Code Online (Sandbox Code Playgroud)

我在浏览器控制台中收到以下错误:

未捕获的TypeError:无法解析模块说明符“ vue”。相对引用必须以“ /”、“./”或“ ../”开头。

如果我的导入行为:

import Vue from '../../node_modules/vue';
Run Code Online (Sandbox Code Playgroud)

然后我得到另一个错误:

http:// localhost:63342 / vue-official-tutorial / node_modules / vue net :: ERR_ABORTED 404(未找到)

我究竟做错了什么?

Evg*_*eny 11

因此,您可以直接在浏览器中使用 ES 模块的方式(截至 2020 年 6 月)是:

  1. 使用您的依赖项的 ESM 版本(具有import而不是 的那个require)。例如,Vue ESM 版本可在以下位置获得:https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-beta.14/vue.esm-browser.js

  2. 让您的浏览器使用实验性importmap功能。导入地图是一种新的网络推荐,主流浏览器尚不支持。https://wicg.github.io/import-maps/#import-map在 Chrome 中这是下chrome://flags#enable-experimental-productivity-features (最新的 Chrome 版本将其移到下chrome://flags#enable-experimental-web-platform-features

  3. importmap在您的 HTML 文件中创建一个。它目前仅适用<script>于 Chrome 中的内联标签。例如:

<script type="importmap">
{ "imports": {
  "vue":        "https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.0-beta.14/vue.esm-browser.js",
  "vue-router": "https://cdnjs.cloudflare.com/ajax/libs/vue-router/4.0.0-alpha.12/vue-router.esm.js",
  "vuex":       "https://cdnjs.cloudflare.com/ajax/libs/vuex/4.0.0-beta.2/vuex.esm-browser.js"
} }
</script>
Run Code Online (Sandbox Code Playgroud)
  1. 加载您自己的代码作为 ESM 模块。
<script type="module" src="./main.js"></script>
Run Code Online (Sandbox Code Playgroud)
  1. 在您自己的脚本以及您导入的脚本中 - 您现在可以从命名模块成功导入。

完整示例:

<script type="module" src="./main.js"></script>
Run Code Online (Sandbox Code Playgroud)


IVO*_*LOV 6

如果你正在使用ES6,那么你不应该手动插入你的main.jsindex.html-这将被处理的WebPack。实际上,Vue最简单的教程是这样的:

  1. npm install -g vue-cli
  2. vue初始化webpack my_project
  3. npm run dev(并开始开发-结果位于http:// localhost:8080
  4. npm run build(结果位于./dist项目文件夹内

另外,您应该这样导入Vue

从'vue'导入Vue;

而不是这样

从'../../node_modules/vue'导入Vue;

编辑

好的,如果您坚持要遵循初学者的方法,而不是使用Webpack和单文件Vue组件-那么您应该这样开始:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

  <title>My beginners project</title> 

  <link rel="stylesheet" type="text/css" href="/assets/css/styles.css" /> 
</head>
<body> 
  <div id="app">
    <router-view></router-view> 
  </div>

  <!-- templates for your components -->
  <template id="login">
    <div>test</div>
  </template>

  <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script> 

  <!-- code for your components -->
  <script type="text/javascript" src="/app/login.js"></script> 

  <!-- Vue Root component should be last -->
  <script type="text/javascript" src="/app/app.js"></script> 
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

您的/app/app.js外观将如下所示:

  var badRoute = Vue.component('bad-route',
  {
    template: '<div id="bad_route"><h1>Page Not Found</h1><p>Sorry, but the page you were trying to view does not exist.</p></div>'
  });

  var vue_router = new VueRouter({
    base: '/app',
    mode: 'hash',
    routes:
    [
      {
        path: '/',
        redirect: '/login'
      },
      {
        path: '/login',
        component: loginForm,
        name: 'LOGIN'
      },

      {
        path: '*', // should be last, otherwise matches everything
        component: badRoute,
        name: 'NOT FOUND'
      }
    ]
  });

  // Main application
  var vue_app = new Vue(
  {
    router: vue_router,
  }).$mount('#app');
Run Code Online (Sandbox Code Playgroud)

您的/app/login.js组件将如下所示:

  var loginForm = Vue.component('login-form',
  {
    template: '#login', // should match the ID of template tag
    data: function()
    {
      var a =
      {
        username: '',
        password: '',
      };
      return a;
    },
    methods:
    {
    }
  });
Run Code Online (Sandbox Code Playgroud)

  • 抱歉。我仍然不明白为什么我按照以上所述无法正常工作。我没有安装vue-cli,因为官方教程明确指出,初学者不应走那条路。为什么我不能在我的`main.js`文件中使用'import'从Vue导入Vue并只是正确地渲染了?(我不是在向您“抱怨”,这是我的实际问题) (5认同)
  • 对不起。我敢肯定我缺少什么,但是我不知道这怎么回答我的问题。如果我只是简单地删除了main.js脚本标记的手动导入,那么...什么都不会发生。 (4认同)