简体   繁体   English

无法从导入中引入 Vue,但如果我将其引入 html 文件中,则可以访问它

[英]Can't bring in Vue from import but can access it if I bring it in html file

So I'm switching from React over to Vue and have just a simple hello world sort of project up that works only when I bring in Vue in script tag in my HTML file.所以我从 React 切换到 Vue,并且只有一个简单的 hello world 项目,只有当我在 HTML 文件的script标签中引入 Vue 时才有效。 However I can't import the node module like:但是我不能像这样导入节点模块:

import './node_modules/vue/dist/vue.js

or或者

import Vue from 'vue'

Like I said if I have a script in my HTML file such as就像我说的,如果我的 HTML 文件中有一个脚本,例如

<script src='./node_modules/vue/dist/vue.js'></script>
<script src="app.js"></script>

Then I can have in my app.js :然后我可以在我的app.js

 new Vue({
    el: '#app',
    data: {
        message: 'Hello vue'
    }
});

And have it work just fine.让它工作得很好。

Why can I only bring Vue in from a script tag?为什么我只能从脚本标签中引入 Vue?

Notes that might or might not matter : I used NPM and installed Vue through NPM it is listed in my dependencies.可能重要或不重要的注意事项:我使用 NPM 并通过 NPM 安装了 Vue,它列在我的依赖项中。 I'm not using webpack or any sort of transpiler like babel.我没有使用 webpack 或任何类型的转译器,如 babel。

<script src="app.js" type="module"></script>

You have to declare the script as of type "module" to tell the browser it's an ES6 module file and then you can use the import keyword assuming your browser supports it.您必须将脚本声明为“module”类型,以告诉浏览器它是一个 ES6 模块文件,然后假设您的浏览器支持它,您就可以使用import关键字。 Browser support: https://caniuse.com/#feat=es6-module浏览器支持: https : //caniuse.com/#feat=es6-module

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM