可以在Angular ts文件中创建函数吗?

问题描述 投票:0回答:1

我对Angular和打字稿(ts)文件还很陌生。

是否可以在ts文件中创建函数,而不必编写重复的代码?

例如,

describe('Navigating to My Page', () => {

  beforeAll(done => {
    myPage = new MyPage();

    myPage.navigate().then(() => {
    done();
  });

  it('user can update stuff', done => {
     //Blah blah blah
  });
});


describe('Navigating to Another Page', () => {

  beforeAll(done => {
    anotherPage = new AnotherPage();

    anotherPage.navigate().then(() => {
    done();
  });

});


//Now, I want to navigate back to My Page.  Is there a way to do this without writing the exact same code over again? 
describe('Navigating to My Page', () => {

  beforeAll(done => {
    myPage = new MyPage();

    myPage.navigate().then(() => {
    done();
  });

  it('user can update stuff', done => {
     //Blah blah blah
  });
});

谢谢!

angular protractor
1个回答
0
投票

是的,这是一个常规功能:

function generalBeforeAll<T>(page: T, done: () => void) {
  page.navigate().then(() => done());
}

describe('Navigating to My Page', () => {

  beforeAll(done => {
    myPage = new MyPage();
    generalBeforeAll<MyPage>(myPage,done);
  });

  it('user can update stuff', done => {
     //Blah blah blah
  });
});


describe('Navigating to Another Page', () => {

  beforeAll(done => {
    anotherPage = new AnotherPage();
    generalBeforeAll<AnotherPage>(anotherPage,done);
  });

});

//Now, I want to navigate back to My Page.  Is there a way to do this without writing the exact same code over again? 
describe('Navigating to My Page', () => {

  beforeAll(done => {
    myPage = new MyPage();
    generalBeforeAll<MyPage>(myPage,done);
  });

  it('user can update stuff', done => {
     //Blah blah blah
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.