顯示具有 TypeScript 標籤的文章。 顯示所有文章
顯示具有 TypeScript 標籤的文章。 顯示所有文章

2017年2月8日 星期三

Step By Step : Vue.js 2 + TypeScript

如何使用 TypeScript 撰寫 Vue.js 2

  1. 建立專案資料夾 lab01,並初始化套件管理定義檔 package.json.
    npm init -y:表示定義檔內容採用預設值.
    D:\vue_lab>mkdir lab01
    D:\vue_lab>cd lab01
    D:\vue_lab\lab01>npm init -y
    Wrote to D:\vue_lab\lab01\package.json:

    {
      "name": "lab01",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
  2. 開始安裝套件
    相依套件:
    D:\vue_lab\lab01>npm install vue --save      (v2.1.0) 安裝 vuejs
    D:\vue_lab\lab01>npm install vue-class-component --save   (4.4.0)ES/TypeScript decorator 用以類別的方式撰寫 component
    開發時期相依套件:
    D:\vue_lab\lab01>npm install typescript --save-dev  (v2.1.5)安裝 typescript。將 ts 編譯為 js 檔
    D:\vue_lab\lab01>npm install webpack --save-dev   (v2.2.1)安裝 webpack module loader。將 js 檔打包
    D:\vue_lab\lab01>npm install webpack-dev-server --save-dev  (v2.3.0)安裝 webpack 開發伺服器
    D:\vue_lab\lab01>npm install concurrently --save-dev  (v3.1.0)安裝concurrently。可同時執行多個命令
  3. 設定 package.json 檔案中 script 區段,最後文件如下
    {
      "name": "lab01",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "build": "tsc -w",
        "server": "webpack-dev-server --open --inline --hot",
        "start": "concurrently \"npm run build\" \"npm run server\""
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "vue": "^2.1.10",
        "vue-class-component": "^4.4.0"
      },
      "devDependencies": {
        "concurrently": "^3.1.0",
        "typescript": "^2.1.5",
        "webpack": "^2.2.1",
        "webpack-dev-server": "^2.3.0"
      }
    }

    tsc -w :監視ts檔案,當發生變更立即編譯
    webpack-dev-server --open --inline --hot : 啟動 dev server,選項可查文件
  4. 在根目錄下新增 tsconfig.json
    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "es2015", "dom", "es2015.promise" ],
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true
      }
    }
  5. 在根目錄下新增 webpack.config.js
    module.exports = {
        entry: './app.js',
        output: {
            publicPath: '/',
            filename: 'app.bundle.js'
        },
        resolve: {
            alias: {
                'vue$': 'vue/dist/vue.common.js'
            }
        },
    }

    將 app.js 打包成 app.bundle.js
  6. 在根目錄下新增 app.ts
    import Vue = require('vue');
    import Component from 'vue-class-component'

    @Component
    class App extends Vue{
        //data
        name = "max";
        counter = 0;

        //computed
        get message(){
            return `${this.name} - ${this.counter}`;
        }

        //methods
        add(){
            this.counter++;
        }
    }

    var app = new App({ el: "#app"});
  7. 在根目錄下新增 index.html
    <html>
        <body>
            <div id="app">
                {{message}}
                <button type="" @click="add">add</button>
            </div>
            <script src="./app.bundle.js"></script>
        </body>
    </html>
  8. 執行 stat 命令,啟動 tsc 監視與啟動 webpack dev server ,並在 browser 中查看結果運作是否正常
    D:\vue_lab\lab01> npm start



資源:

2016年4月20日 星期三

將 TypeScript 檔案編譯至特定的位置

 ASP.NET Core 樣版的預設目錄結構如下
  • wwwroot:是用來存放與網站有關的靜態檔案
所以 TypeScript(*.ts) 的檔案就不應該放在 wwwroot 下面.

假設我正在寫一個 ng 的專案,我在專案 root 下建立一個名為 ngApp 的資料夾,用來存放 TypeScript 檔案,這個資料夾有自己的目錄結構,我希望編譯出來的 js 檔案可以放在 wwwroot/scripts 下面,該如何做呢?

圖片說明

1.希望編譯出的 ts 放在 wwwroot/script 下
2.開發 ng app 的 TypeScript 檔



可以在專案目錄下建立一個  tsconfig.json 檔案,compiler 會根據裡面的定義來處理,檔案內容如下
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "rootDir": "",
    "outDir": "./wwwroot/scripts"
  },
    "exclude": [
        "node_modules",
        "wwwroot"
    ]
}


必需在 compilerOptions 屬性下設定 rootDir,outDir 這2個屬性
  • rootDir: 告訴 compiler 要編譯的 ts 檔案的根目錄在哪.
                   通常就是專案下所有的 ts檔,所以設定空白.
  • outDir:  告訴 compiler 編譯後產生的 js 檔要放在哪.
                   這邊指定在 wwwroot/scripts 目錄下
這邊要注意的是,如果只設定 outDir 而沒有設定 rootDir,則整個專案下的 ts 檔所編譯出來的 js 都會放在 wwwroot/scripts 下面,所以你想要output出來的目錄結構跟開發是相同的,必需設 定 rootDir 屬性才行.