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>
  • 相关阅读:
    intel instruction 指令速查
    WinDbg双机调试配置
    MSDN上关于WinDbg的手册
    build temu error about SDL
    taintCheck的实现
    Vim使用taglist功能
    Windows编写driver
    cabal替代脚本
    怎样理解Functor与Monad
    haskell基本语法
  • 原文地址:https://www.cnblogs.com/Jimc/p/10112773.html
Copyright © 2011-2022 走看看