![](/img/trans.png)
[英]Webpack import angular in angular-webpack-workflow demo project
[英]Simple demo project Webpack KO(with a components) javascript
我想用javascript敲除組件構建SPA經過大量的閱讀和擺弄之后,我似乎仍然無法通過webpack獲得有效的javascript(無打字稿)敲除(與組件)項目。 我找到了簡單的淘汰項目,但無法使它們與webpack一起使用。
有人使用Webpack擁有至少一個ko組件的演示項目嗎?
使用Webpack的Yeoman generator-ko-spa(使用JavaScript)會很棒。
日Thnx
從頭開始設置“ Hello world”應用的方法如下:
npm init -y
npm install --save-dev webpack webpack-cli html-loader
npm install --save-dev knockout
"scripts": { "build": "webpack" }
webpack.config.js
文件: const path = require("path");
module.exports = {
entry: path.resolve(__dirname, "index.js"),
module: {
rules: [
// This tells webpack to import required html files
// as a string, through the html-loader
{ test: /\.html$/, use: [ "html-loader" ] }
],
},
// You *could* include knockout in your bundle,
// but I usually get it from a CDN
externals: {
knockout: "ko"
}
}
Components
的文件夾 Greeter.html
<h1 data-bind="text: message">...</h1>
Greeter.js
const greeterTemplate = require("./Greeter.html");
module.exports = {
viewModel: function(params) {
this.message = params.message;
},
template: greeterTemplate
};
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<greeter params="message: 'Hello world!'"></greeter>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="dist/main.js"></script>
</body>
</html>
index.js
文件 const ko = require("knockout");
const greeter = require("./Components/Greeter");
ko.components.register("greeter", greeter);
ko.applyBindings({});
npm run build
,webpack將在dist
文件夾中創建一個文件 index.html
。 它應該以“ Hello world”打招呼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.