2017-06-15 64 views
1

App.ts屬性不使用打字稿

import { $, WebElement } from 'protractor'; 
export class App { 

    public appName: WebElement; 

    constructor() { 
    this.appName = $('#appName'); 
    } 
} 

app.e2e.test.ts

import { browser, $ } from 'protractor'; 
import { App } from '../pageobjects/App' 
const BASE_URL = 'http://localhost:1344'; 

describe('App',() => { 

    beforeEach(() => { 
    browser.get(BASE_URL); 
    }); 

    it('should load the Sites UI homepage',() => { 
    console.log(App.appName); 
    }); 
}); 

任何想法,爲什麼我不能訪問App.ts定義的屬性類型上存在?

錯誤:

> tsc -p tsconfig.test.json && protractor dist/protractor.config.js 

test/e2e/app.e2e.test.ts(15,28): error TS2339: Property 'appName' does not exist on type 'typeof App'. 
+2

您正試圖在typeof應用上訪問它,而不是在類的實例上。您需要在訪問屬性之前創建該類的實例。也許讀一下關於類和類實例之間的區別會有所幫助。 :) https://www.quora.com/What-is-difference-between-an-object-a-class-and-an-instance – toskv

回答

2
在你的異常

它很清楚什麼是錯的:

property 'appName' does not exist on type 'typeof App'. 

這個工作,你需要申報的App像這樣一個新的實例:

describe('App',() => { 
    app: App; 

    beforeEach(() => { 
    browser.get(BASE_URL); 
    app = new App(); 
    }); 

    it('should load the Sites UI homepage',() => { 
    console.log(app.appName); 
    }); 
});