简体   繁体   English

Vue.js在组件中使用本地javascript文件功能:未捕获的TypeError:__WEBPACK_IMPORTED_MODULE_0 __。writeSomething不是函数

[英]Vue.js using local javascript file functions in component: Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0__.writeSomething is not a function

I am trying to import a local JS file into a single file component in a Vue application. 我正在尝试将本地JS文件导入Vue应用程序中的单个文件组件。 My application is a scaffold generated by vue-CLI (ver 3.8.2). 我的应用程序是由vue-CLI(版本3.8.2)生成的支架。

Here are my relevant code snippets (all other code in application is unchanged from generated code): 这是我相关的代码片段(应用程序中的所有其他代码与生成的代码相同):

/path/to/app-vue/src/components/HelloWorld.vue /path/to/app-vue/src/components/HelloWorld.vue

<template>
  <div class="hello">
    <Module1/>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Module1 from './Module1.vue';

@Component({
  components: {
    Module1,
  }
})
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

/path/to/vue-app/src/components/Module1.vue /path/to/vue-app/src/components/Module1.vue

<template>
    <div class="module1">
        Module2
    </div>
</template>

<script>
import { Component, Prop, Vue } from 'vue-property-decorator';
import Module2 from './Module2.vue';

@Component({
  components: {
    Module2,
  },
})
export default class Module1 extends Vue {
}
</script>

/path/to/vue-app/Module2.vue /path/to/vue-app/Module2.vue

<template>
    <div id='demo'>
    </div>
</template>

<script>
import foo from '../assets/js/foo';

foo.writeSomething('#demo');
</script>

/path/to/vue-app/src/assets/js/foo.js /path/to/vue-app/src/assets/js/foo.js

function writeSomething(el) {
  elem = window.document.getElementById(el);
  elem.innerHTML = 'Hello World!';
}

export default {
    writeSomething
}

When I run npm run serve and navigate to '/' in the browser, I get the following error messages in the console: 当我运行npm run serve并在浏览器中导航到“ /”时,在控制台中收到以下错误消息:

"export 'default' (imported as 'mod') was not found in '-!../../node_modules/cache-loader/dist/cjs.js??ref--12-0!../../node_modules/babel-loader/lib/index.js!../../node_modules/cache-loader/dist/cjs.js??ref--0-0!../../node_modules/vue-loader/lib/index.js??vue-loader-options!./Module2.vue?vue&type=script&lang=js&' “在'-!../../ node_modules / cache-loader / dist / cjs.js ?? ref--12-0!../。中找不到导出'默认'(导入为'mod')。 /node_modules/babel-loader/lib/index.js!../../node_modules/cache-loader/dist/cjs.js??ref--0-0!../../node_modules/vue-loader /lib/index.js??vue-loader-options!./Module2.vue?vue&type=script&lang=js&'

And in the browser DevTools console, I get the following stack trace: 在浏览器DevTools控制台中,我得到以下堆栈跟踪:

Uncaught TypeError: _assets_js_foo__WEBPACK_IMPORTED_MODULE_0__.writeSomething is not a function
    at eval (Module2.vue?df9f:9)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/Module2.vue?vue&type=script&lang=js& (app.js:1078)
    at __webpack_require__ (app.js:767)
    at fn (app.js:130)
    at eval (Module2.vue?35cf:1)
    at Module../src/components/Module2.vue?vue&type=script&lang=js& (app.js:3448)
    at __webpack_require__ (app.js:767)
    at fn (app.js:130)
    at eval (Module2.vue?6104:1)
    at Module../src/components/Module2.vue (app.js:3436)

How do I load local javascript files into a Single File Component, and use functions defined in the loaded Javascript (within the component)? 如何将本地javascript文件加载到“单个文件”组件中,并使用在已加载的Javascript中定义的函数(在组件内部)?

You need to explicitely export the function and import it using its name. 您需要显式export函数并使用其名称导入。

Module2.vue Module2.vue

import { writeSomething } from '../assets/js/foo.js';
writeSomething('#demo');

export default { ...

foo.js foo.js

export function writeSomething(el) {
  elem = window.document.getElementById(el);
  elem.innerHTML = 'Hello World!';
}

If you are using typescript, make sure that you can import js modules 如果您使用的是打字稿,请确保可以导入js模块

you can also export a default module 您还可以导出默认模块

function writeSomething(el) {
  elem = window.document.getElementById(el);
  elem.innerHTML = 'Hello World!';
}


export default {
    writeSomething
}

and import it as 并导入为

import foo from '../assets/foo.js';

// ...
foo.writeSomething('#demo');

Export default should specify a name like below: 导出默认值应指定如下名称:

export default writeSomething;

Named Exports 命名出口

  • Can export multiple values 可以导出多个值
  • MUST use the exported name when importing 导入时必须使用导出的名称

Default Exports 默认出口

  • Export a single value 导出单个值
  • Can use any name when importing 导入时可以使用任何名称

You can see more about Named Export and Default Export here 您可以在此处查看有关命名导出和默认导出的更多信息

暂无
暂无

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

相关问题 Vue.js-未捕获(承诺)TypeError:__WEBPACK_IMPORTED_MODULE_0__services_Api__.a.post不是函数 - Vue.js - Uncaught (in promise) TypeError: __WEBPACK_IMPORTED_MODULE_0__services_Api__.a.post is not a function 在vue.js中将导入的sass文件与渲染功能一起使用 - using imported sass file with render functions in vue.js 未捕获的类型错误:_firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth 不是 function - Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.app.auth is not a function "未捕获的 TypeError: (0 , react_router_dom__WEBPACK_IMPORTED_MODULE_2__.useHistory) 不是函数 (React.js)" - Uncaught TypeError: (0 , react_router_dom__WEBPACK_IMPORTED_MODULE_2__.useHistory) is not a function (React.js) 尝试通过vue和Webpack安装keycloak-js时出现未捕获的类型错误:__WEBPACK_IMPORTED_MODULE_3_keycloak_js__不是函数 - Uncaught type error when trying to install keycloak-js with vue and webpack: __WEBPACK_IMPORTED_MODULE_3_keycloak_js__ is not a function 未捕获的类型错误:__WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext 不是 function - Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function 未捕获的类型错误:_firebase__WEBPACK_IMPORTED_MODULE_3__.default.collections 不是 function - Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_3__.default.collections is not a function 未捕获的语法错误:无法在模块 Vue.js 单个组件文件之外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module Vue.js single component file Vue.js:未捕获(承诺)类型错误:$set 不是函数 - Vue.js: Uncaught (in promise) TypeError: $set is not a function 如何在 vue.js 组件中使用导入的 javascript 库构造函数? - How to use imported javascript library constructor in vue.js component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM