繁体   English   中英

Vue导入导致无法分配给object错误的只读属性'exports'

[英]Vue import causing cannot assign to read only property 'exports' of object error

我目前正在将 HTML 模板迁移到 Vue。 现在我开始导入存储在src中的 main.js 文件中的 Javascript 文件。

我的文件如下所示:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";

import pageLoader from "./assets/js/global/page-loader";
import tooltips from "./assets/js/global/tooltips";

Vue.use(pageLoader);
Vue.use(tooltips);

Vue.config.productionTip = false;

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");

每个 Javascript 文件都位于src/js文件夹内

但是一旦我运行应用程序,我就会收到以下错误:

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Module.eval (webpack-internal:///./src/assets/js/utils/plugins.js:56)
    at eval (webpack-internal:///./src/assets/js/utils/plugins.js:57)
    at Module../src/assets/js/utils/plugins.js (app.js:1579)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at eval (webpack-internal:///./src/assets/js/global/tooltips.js:1)
    at Object../src/assets/js/global/tooltips.js (app.js:1556)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at eval (webpack-internal:///./src/main.js:15)

整个项目可以在以下链接上的 github 上看到:链接到 github

是什么导致了这个错误? 我将不胜感激任何形式的帮助来解决我的问题。

您的文件没有导出 Vue 尝试使用的功能。

您需要做的是导出您在文件中找到的 function。

请参阅本指南以在 VueJs 中导出函数: https://medium.com/js-dojo/a-beginners-guide-to-exports-in-es6-af27b5a621d0

这将帮助您了解如何从 Vue 将用来运行的这些文件中导出函数。

如何修复“无法分配给 object 的只读属性“导出”'#<object> ' 将第二个 function 添加到 module.exports 时?<div id="text_translate"><p> 不幸的是,我已经被困了大约 5 天了。 我用谷歌搜索过,没有什么对我有用。 我有一个 utils.js 文件,里面有很多东西可以帮助我。 当我将 function "doesUserContainRoles" 添加到导出时,我的 Chrome 控制台给了我错误:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 当我删除 function 时,utils 文件中的所有内容都可以完美运行,根本没有 0 个问题。 只要我添加 function,我就会收到此错误。 我通过以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 并且没有与此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 这是错误堆栈:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也尝试使用export default 。 这样做时,我的前端工作。 网站加载没有错误。 不过,我的后端使用 Express.js 运行,然后引发错误:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object>

[英]How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports?

类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object>

[英]Type Error: Cannot assign to read only property 'backgroundColor' of object '#<Object>'

暂无
暂无

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

相关问题 未捕获的TypeError:无法分配给对象'#<Object>'的只读属性'exports' TypeError:无法分配为只读对象“#”的属性“ exports” <Object> 在ReactJS中 无法分配给对象“#”的只读属性“exports”<Object> &#39; 未捕获的TypeError:无法分配为仅读取对象&#39;的属性&#39;exports&#39; 尝试在 React 中使用“Socket.io-client”模块会引发错误:无法分配给对象“#”的只读属性“exports”<Object> &#39; 如何修复无法分配给 object 的只读属性 'exports' '#<object> ',这是使用framework7-svelte cli<div id="text_translate"><p> <a href="https://i.stack.imgur.com/XCz77.png" rel="nofollow noreferrer">这个错误 module.exports</a></p><p> <a href="https://i.stack.imgur.com/ZlhAp.jpg" rel="nofollow noreferrer">这 webpack</a>通天塔</p></div></object> 如何修复“无法分配给 object 的只读属性“导出”'#<object> ' 将第二个 function 添加到 module.exports 时?<div id="text_translate"><p> 不幸的是,我已经被困了大约 5 天了。 我用谷歌搜索过,没有什么对我有用。 我有一个 utils.js 文件,里面有很多东西可以帮助我。 当我将 function "doesUserContainRoles" 添加到导出时,我的 Chrome 控制台给了我错误:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 当我删除 function 时,utils 文件中的所有内容都可以完美运行,根本没有 0 个问题。 只要我添加 function,我就会收到此错误。 我通过以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 并且没有与此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 这是错误堆栈:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也尝试使用export default 。 这样做时,我的前端工作。 网站加载没有错误。 不过,我的后端使用 Express.js 运行,然后引发错误:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object> 我该如何解决在React中无法分配为只读对象的属性“ exports”? 类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object> 无法分配给对象 &#39;# 的只读属性<Object> &#39;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM