gpt4 book ai didi

javascript - Uncaught Error : Target container is not a DOM element.(React 网页不会显示任何内容)

转载 作者:行者123 更新时间:2023-11-29 10:58:03 27 4
gpt4 key购买 nike

我一直在关注 Meteor 提供的教程但无法正确运行提供的代码。启动应用程序后,页面上没有显示任何内容。可能是什么问题呢?

我使用的是 Meteor 平台版本 1.7。我已经使用命令安装了 React 依赖项:

meteor npm install --save react react-dom

我在浏览器控制台中也遇到了错误:

Uncaught Error: Target container is not a DOM element.
at invariant (modules.js?hash=bbd33713066b742f1084320f9ea66e4052f533c1:3462)
at legacyRenderSubtreeIntoContainer (modules.js?hash=bbd33713066b742f1084320f9ea66e4052f533c1:22109)
at render (modules.js?hash=bbd33713066b742f1084320f9ea66e4052f533c1:22190)
at Meteor.startup (main.js:8)
at maybeReady (meteor.js?hash=0504f43f667698535416b00eb44eb6f53161cb63:927)
at HTMLDocument.loadingCompleted (meteor.js?hash=0504f43f667698535416b00eb44eb6f53161cb63:939)

package.json:

{
"name": "test",
"private": true,
"scripts": {
"start": "meteor run",
"test": "meteor test --once --driver-package meteortesting:mocha",
"test-app": "TEST_WATCH=1 meteor test --full-app --driver-package meteortesting:mocha",
"visualize": "meteor --production --extra-packages bundle-visualizer"
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"meteor-node-stubs": "^0.4.1",
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"meteor": {
"mainModule": {
"client": "client/main.js",
"server": "server/main.js"
},
"testModule": "tests/main.js"
}
}

My file structure:

ma​​in.html:

<head>
<title>Todo List</title>
</head>

<body>
<h1>Hello!</h1>
<div id="render-target"></div>
</body>

ma​​in.js:

import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';

import App from '../imports/ui/App.js';

Meteor.startup(() => {
render(<App />, document.getElementById('render-target'));
});

ma​​in.css:

body {
padding: 10px;
font-family: sans-serif;
}

App.js:

import React, { Component } from 'react';

import Task from './Task.js';

// App component - represents the whole app
export default class App extends Component {
getTasks() {
return [
{ _id: 1, text: 'This is task 1' },
{ _id: 2, text: 'This is task 2' },
{ _id: 3, text: 'This is task 3' },
];
}

renderTasks() {
return this.getTasks().map((task) => (
<Task key={task._id} task={task} />
));
}

render() {
return (
<div className="container">
<header>
<h1>Todo List</h1>
</header>

<ul>
{this.renderTasks()}
</ul>
</div>
);
}
}

Task.js:

import React, { Component } from 'react';

// Task component - represents a single todo item
export default class Task extends Component {
render() {
return (
<li>{this.props.task.text}</li>
);
}
}

错误是:

Uncaught Error: Target container is not a DOM element.

最佳答案

如下更新main.js文件

import React from 'react';
import { render } from 'react-dom';
import { Meteor } from 'meteor/meteor';

import App from '../imports/ui/App.js';

import './main.html'

我已经导入了 main.html & 它应该可以工作。

同样没有上面的,你可以像这样删除 blaze-template

meteor remove blaze-html-templates

然后像下面这样添加静态

meteor add static-html

关于javascript - Uncaught Error : Target container is not a DOM element.(React 网页不会显示任何内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com