Javascript 我不知道';我不知道如何在svelte.js中使用生命周期 假设

Javascript 我不知道';我不知道如何在svelte.js中使用生命周期 假设,javascript,node.js,npm,yarnpkg,svelte,Javascript,Node.js,Npm,Yarnpkg,Svelte,我对前端不太了解 想做什么 我想使用生命周期与苗条 发生错误 发生以下错误,生命周期方法不可用: ERROR in ./node_modules/svelte/index.mjs Module not found: Error: Can't resolve './internal' in 'C:\project\test_svelte_lifecycle\node_modules\svelte' @ ./node_modules/svelte/index.mjs 1:0-167 1:0-167

我对前端不太了解

想做什么 我想使用生命周期与苗条

发生错误 发生以下错误,生命周期方法不可用:

ERROR in ./node_modules/svelte/index.mjs
Module not found: Error: Can't resolve './internal' in 'C:\project\test_svelte_lifecycle\node_modules\svelte'
 @ ./node_modules/svelte/index.mjs 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167 1:0-167
 @ ./test-component.svelte
 @ ./app.js
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
这是什么原因

我从公式中得到了这个代码。 但是,发生了一个错误,它不起作用。 我不知道为什么

有人犯了同样的错误,导致github问题

我相应地修改了代码,但它不起作用

代码 package.json

{
  "dependencies": {
    "svelte": "^3.17.2",
    "svelte-loader": "^2.13.6",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10"
  },
  "peerDependencies": {
    "svelte": "^3.17.2"
  }
}

webpack.config.js

const path=require('path');
module.exports={
条目:path.resolve(uu dirname,'app.js'),
输出:{
path:path.resolve(uu dirname,'./dist/'),
文件名:“bundle.js”
},
决心:{
别名:{
svelte:path.resolve('node_modules','svelte')
},
扩展名:['.svelte'],
main字段:['svelte'、'browser'、'module'、'main']
},
模块:{
规则:[
{
测试:/\(html |苗条)$/,
排除:/node_模块/,
使用:{
加载器:“苗条加载器”,
选项:{
customElement:true
}
}
}
]
}
};

测试组件。苗条

<script>
  import { onMount } from "svelte";

  let hello = "hello";
  let world = "world";

  onMount(async () => {
    hello = "hello hello";
  });
</script>

<svelte:options tag="test-component" />

<div>{hello} and {world}</div>

index.html

<html>

<head>
    <meta charset="utf8">
    <script src="./dist/bundle.js"></script>
</head>

<body>
    <test-component></test-component>
</body>

</html>

我认为您的错误是因为您的网页
解析.extensions
中没有
.mjs
。由错误消息
/internal
选中的文件实际上是一个
.mjs
文件,但您只有
.svelte
扩展名

应该是这样的:

解析:{
别名:{
svelte:path.resolve('node_modules','svelte'),
},
扩展名:['.mjs','.js','.svelte'],
main字段:['svelte'、'browser'、'module'、'main'],
},

我认为您的错误与生命周期无关,而是与绑定器和模块导入有关。您是否尝试创建一个简单的组件并将其导入到主组件中,以查看项目是否将生成。谢谢您的回答。正如您所说,它现在可以通过添加代码来编译webpack.config!非常感谢。