zoukankan      html  css  js  c++  java
  • ES6 export,import报错

    问题描述:

    现有两个文件:

    profile.js

    const firstName = 'Michael';
    const lastName = 'Jackson';
    const year = 2018;
    export {firstName, lastName, year}

    test.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <script>
        import {firstName, lastName, year} from './profile.js';
        console.log(firstName);
        console.log(lastName);
        console.log(year);
    </script>
    </body>
    </html>

    运行结果:

    test.html:9 Uncaught SyntaxError: Unexpected token {

    问题解答:

    在HTML文件中不能使用export,import,需要在webpack构建项目中使用,并且只作用于.vue.js文件。

    如果非要使用且浏览器支持ES6,需要加上 type="module"

    <script type="module">
        import {firstName, lastName, year} from './profile.js';
        console.log(firstName);
        console.log(lastName);
        console.log(year);
    </script>
  • 相关阅读:
    合理的嵌入式开发学习路线
    Nginx
    RARP
    强弱电共地
    ACDC
    各电脑进Bios方法
    Java中Integer.parseInt
    全排列
    Java实现LRU缓存方案?
    缓存有关的几个问题
  • 原文地址:https://www.cnblogs.com/Jimc/p/10112773.html
Copyright © 2011-2022 走看看