angularjs2 使用webpack配置装饰器(实现方法装饰器及类装饰器)

1、首先创建一个文件夹

如ng2_deco

2、cmd 进入ng2_deco中 执行命令 npm init,会在ng2_dec目录下生成package.json

3、配置package.json文件,内容如下

{
  "name": "ng2_deco",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config webpack.publish.config.js",
    "start": "webpack-dev-server --config webpack.dev.config.js --devtool -eval --progress --color --hot --content-base src"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "awesome-typescript-loader": "^3.2.1",
    "webpack": "^3.0.0",
    "webpack-dev-server": "^2.5.0"
  },
  "dependencies": {
    "reflect-metadata": "^0.1.10",
    "tslint": "^3.15.1",
    "typescript": "~2.2.0"
  }
}

4、创建tsconfig.json文件,配置内容如下

{
  "compilerOptions": {
    "target": "es5",
	"module": "es2015",
	"moduleResolution": "node",
	"sourceMap": true,
	"emitDecoratorMetadata": true,
    "experimentalDecorators": true,
	"lib": [ "es2015","dom" ],
    "noImplicitAny": true,
	"suppressImplicitAnyIndexErrors": true,
	"typeRoots": [ "node_modules/@types/" ]
  },
  "exclude": [ "node_modules" ]
}

5、创建webpack.dev.config.js文件,内容如下

let webpack = require('webpack');
let path = require('path');
module.exports = {
    entry: path.resolve(__dirname,'./index.ts'),
    output:{
        path:path.resolve(__dirname, 'dist'),
        filename:'index.js'
    },
    resolve: {
		extensions: ['.js','.ts']
	},
    module:{
        rules: [
            {
                test:/\.js?$/,
				loaders: [
				   {
					   loader: 'awesome-typescript-loader',
					   options: {configFileName:path.resolve(__dirname) + '/tsconfig.json'}
				   }
				]
            }
        ]
    },
	plugins: [
		new webpack.NodeEnvironmentPlugin(),
	]
};

6、在cmd进入ng2_deco目录下 执行 npm install,会生成node_modules目录

7、再次执行 npm install webpack --save-dev,到此 安装完毕

8、方法装饰器

1)创建html、ts文件

<html>

<head>
<title>装饰器</title>
<script src="index.js"></script>
</head>

<body> 

</body>

</html>
function methodDec(){
	return function(){
		console.log("bbb");
	}
}

class Test{
	
   @methodDec()
    method(){
		
	}	
}

2)使用tsc编译ts文件


发现报错 没有关系,打开浏览器,访问文件


9、类加载器

function classDec< T extends {new(...args:any[]):{}}>(constructor:T){
	return class extends constructor{
		a = "aa";
		b = "bb";
	}
}
@classDec
class Test{
	name:string;
	constructor(name:string){
		this.name = name;
	}
}
console.log(new Test("ng2"));

注意:类中是不能改变修饰器中的值的

将代码修改为

function classDec< T extends {new(...args:any[]):{}}>(constructor:T){
	return class extends constructor{
		a = "aa";
		b = "bb";
	}
}
@classDec
class Test{
	name:string;
	a:string;
	constructor(name:string,a:string){
		this.name = name;
		this.a = a;
	}
}
console.log(new Test("ng2","no"));

发现a的结果还是aa而不是no。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Webpack是一个现代化的前端构建工具,它可以帮助开发者将多个模块打包成一个或多个最终的静态资源文件。下面是Webpack配置和基本使用方法: 1. 安装Webpack:可以通过npm或者yarn安装Webpack。在命令行中执行以下命令: ``` npm install webpack webpack-cli --save-dev ``` 2. 创建Webpack配置文件:在项目根目录下创建一个名为`webpack.config.js`的文件,该文件是Webpack配置文件。 3. 配置入口和出口:在`webpack.config.js`中配置入口和出口。入口是指Webpack开始构建的入口文件,出口是指Webpack构建后生成的输出文件。例如: ```javascript // webpack.config.js const path = require('path'); module.exports = { entry: './src/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' } }; ``` 4. 配置加载器(Loaders):Webpack支持使用加载器来处理各种型的文件,例如将ES6代码转换为ES5、将SCSS转换为CSS等。可以在`webpack.config.js`中配置加载器。例如,使用Babel加载器处理ES6代码: ```javascript // webpack.config.js module.exports = { // ... module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader' } } ] } }; ``` 5. 配置插件(Plugins):Webpack还支持使用插件来进行更高级的功能扩展。可以在`webpack.config.js`中配置插件。例如,使用HtmlWebpackPlugin插件生成HTML文件: ```javascript // webpack.config.js const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { // ... plugins: [ new HtmlWebpackPlugin({ template: './src/index.html' }) ] }; ``` 6. 运行Webpack:在命令行中执行以下命令来运行Webpack构建项目: ``` npx webpack ``` 以上是Webpack的基本配置使用方法,你可以根据自己的需求进行更详细的配置使用。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值