# TypeScript-Vue-Starter **Repository Path**: hqywork/TypeScript-Vue-Starter ## Basic Information - **Project Name**: TypeScript-Vue-Starter - **Description**: https://github.com/Microsoft/TypeScript-Vue-Starter 学习研究及翻译项目。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master-CN - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2018-04-01 - **Last Updated**: 2022-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # TypeScript Vue Starter - TypeScript Vue 入门 This quick start guide will teach you how to get TypeScript and [Vue](https://vuejs.org) working together. This guide is flexible enough that any steps here can be used to integrate TypeScript into an existing Vue project. > [!TRANSLATION] > 这个快速入门指南将教你如何让 TypeScript 和 [Vue](https://vuejs.org) 一起工作。该指南是足够灵活的,可以使用这里讲述的步骤将 TypeScript 集成到现有的 Vue 项目中。 # Initialize your project - 初始化你的项目 Let's create a new package. > [!TRANSLATION] > 让我们创建一个新的包。 ```sh mkdir typescript-vue-tutorial cd typescript-vue-tutorial ``` Next, we'll scaffold our project in the following way: > [!TRANSLATION] > 接着,我们将以下方式作为我们项目的脚手架: ```txt typescript-vue-tutorial/ ├─ dist/ └─ src/ └─ components/ ``` TypeScript files will start out in your `src` folder, run through the TypeScript compiler, then webpack, and end up in a `bundle.js` file in `dist`. Any components that we write will go in the `src/components` folder. > [!TRANSLATION] > 从 `src` 文件夹出发,其中的 TypeScript 文件通过 TypeScript 编译器,然后 webpack 的运行,最后结束于 `dist` 中的 `bundle.js` 文件中。我们编写的任何组件都将放置于 `src/components` 文件夹。 Let's scaffold this out: > [!TRANSLATION] > 让我们把脚手架创建出来: ```shell mkdir src cd src mkdir components cd .. ``` Webpack will eventually generate the `dist` directory for us. > [!TRANSLATION] > Webpack 将最终为我们生成 `dist` 目录。 # Initialize the project - 初始化项目 Now we'll turn this folder into an npm package. > [!TRANSLATION] > 现在,我们将把这个文件夹转变成一个 NPM 包。 ```shell npm init ``` You'll be given a series of prompts. You can use the defaults except for your entry point. You can always go back and change these in the `package.json` file that's been generated for you. > [!TRANSLATION] > 你将得到一系列的提示。除了入口点外,你都可以使用默认值。你常常可以返回,并在为你生成的 `package.json` 文件中修改这些内容。 # Install our dependencies - 安装我们的依赖 Ensure TypeScript, Webpack, Vue and the necessary loaders are installed. > [!TRANSLATION] > 确保 TypeScript、Webpack、Vue 及必要的加载器都已经安装。 ```sh npm install --save-dev typescript webpack ts-loader css-loader vue vue-loader vue-template-compiler ``` Webpack is a tool that will bundle your code and optionally all of its dependencies into a single `.js` file. While you don't need to use a bundler like Webpack or Browserify, these tools will allow us to use `.vue` files which we'll cover in a bit. > [!TRANSLATION] > Webpack 是一个可以将你的代码及其所有的依赖项捆绑到一个单一 `.js` 文件的工具。虽然你不需要使用像 Webpack 或 Browerify 等捆绑器,但这些工具将允许我们使用 `.vue` 文件,我们将在后序进行介绍。 We didn't need to [add `.d.ts` files](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html), but if we were using a package which didn't ship declaration files, we'd need to install the appropriate `@types/` package. [Read more about using definition files in our documentation](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html). > [!TRANSLATION] > 我们不需要添加 [`.d.ts` 文件](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html),但是如果我们使用的包不包含声明文件,那么我们需要安装适当的 `@types` 包。[更多信息请阅读我们文档中有关使用声明文件](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html)。 # Add a TypeScript configuration file - 添加 TypeScript 配置文件 You'll want to bring your TypeScript files together - both the code you'll be writing as well as any necessary declaration files. > [!TRANSLATION] > 你将想在提供 TypeScript 文件的同时,将编写的代码和任何必须的声明文件放到一起。 To do this, you'll need to create a `tsconfig.json` which contains a list of your input files as well as all your compilation settings. Simply create a new file in your project root named `tsconfig.json` and fill it with the following contents: > [!TRANSLATION] > 为此,你将需要创建一个 `tsconfig.json`,它包含了你输入文件的列表以及你的所有编译设置。仅需在你的项目根创建一个 `tsconfig.json` 的文件,并填充下列内容: ```json { "compilerOptions": { "outDir": "./built/", "sourceMap": true, "strict": true, "noImplicitReturns": true, "module": "es2015", "moduleResolution": "node", "target": "es5" }, "include": [ "./src/**/*" ] } ``` Notice the `strict` flag is set to true. At the very least, TypeScript's `noImplicitThis` flag will need to be turned on to leverage Vue's declaration files, but `strict` gives us that and more (like `noImplicitAny` and `strictNullChecks`). We strongly recommend using TypeScript's stricter options for a better experience. > 注意 `strict` 标记被设置为 true。至少,TypeScript 的 `noImplicitThis` 标记将需要打开用来发挥 Vue 的声明文件,但是 `strict` 提供了更多(像 `noImplicitAny` 和 `strictNullChecks`)。我们强烈推荐使用 TypeScript 更严格的选项,以便获得更多的体验。 # Adding Webpack - 添加 Webpack We'll need to add a `webpack.config.js` to bundle our app. > [!TRANSLATION] > 我们将需要添加 `webpack.config.js` 来打包我们的应用程序。 ```js var path = require('path') var webpack = require('webpack') module.exports = { entry: './src/index.ts', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', filename: 'build.js' }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { // Since sass-loader (weirdly) has SCSS as its default parse mode, we map // the "scss" and "sass" values for the lang attribute to the right configs here. // other preprocessors should work out of the box, no loader config like this necessary. // 由于 SASS 加载器(古怪的)将 SCSS 作为它默认的解析模块,所以我们为语言特性映射“scss”和“sass”值以便在这里恰当的配置。 // 其它的预处理程序应该是开箱即用的,没有像这种必要的加载器配置。 'scss': 'vue-style-loader!css-loader!sass-loader', 'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax', } // other vue-loader options go here // 其它 vue-loader 选项 } }, { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/], } }, { test: /\.(png|jpg|gif|svg)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]' } } ] }, resolve: { extensions: ['.ts', '.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js' } }, devServer: { historyApiFallback: true, noInfo: true }, performance: { hints: false }, devtool: '#eval-source-map' } if (process.env.NODE_ENV === 'production') { module.exports.devtool = '#source-map' // http://vue-loader.vuejs.org/en/workflow/production.html module.exports.plugins = (module.exports.plugins || []).concat([ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"production"' } }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ]) } ``` # Add a build script - 添加生成脚本 Open up your `package.json` and add a script named `build` to run Webpack. Your `"scripts"` field should look something like this: > [!TRANSLATION] > 打开你的 `package.json`,并添加一个被命名为 `build` 的脚本来运行 Webpack。你的“scripts”域应该看起来像: ```json "scripts": { "build": "webpack", "test": "echo \"Error: no test specified\" && exit 1" }, ``` Once we add an entry point, we'll be able to build by running > [!TRANSLATION] > 一旦我们添加了一个入口点,我们就可以通过运行以下命令来生成 ```sh npm run build ``` and have builds get triggered on changes by running > [!TRANSLATION] > 并通过运行来触发生成 ```sh npm run build -- --watch ``` # Create a basic project - 创建一个基本项目 Let's create the most bare-bones Vue & TypeScript example that we can try out. First, create the file `./src/index.ts`: > [!TRANSLATION] > 让我们创建一个最简单的使用 Vue 和 TypeScript 的示例。首先,创建文件 `./src/index.ts`: ```ts // src/index.ts import Vue from "vue"; let v = new Vue({ el: "#app", template: `