使用typescript
npm install ts-loader --save
npm install typescript --save
class Greeter {
greeting: string
constructor(message: string) {
this.greeting = message
}
greet() {
return "Hello, " + this.greeting
}
}
let greeter = new Greeter("world")
alert(greeter.greet())
-
webpack.config.js
中对ts文件使用ts-loader
const path = require('path')
module.exports = {
mode: 'development',
entry: './src/index.tsx',
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}]
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname,'build')
}
}
{
"compilerOptions": {
"outDir": "./build",
"module": "es6",
"target": "es5",
"allowJs": true
},
"exclude": [
"node_modules"
]
}
使用三方库
import * as _ from 'lodash'
class Greeter {
greeting: string
constructor(message: string) {
this.greeting = message
}
greet() {
return _.join(["Hello,",this.greeting],' ')
}
}
let greeter = new Greeter("world")
alert(greeter.greet())
网友评论