错误TS2304:找不到名称' page'

时间:2017-08-06 18:41:09

标签: typescript nativescript

这是我的page.ts

page.ts

var viewModel : launchViewModelModule.LaunchViewModel;
viewModel = new launchViewModelModule.LaunchViewModel();

export function navigatingTo(args: observableModule.EventData) {
    var page = <pageModule.Page>args.object;
    page.bindingContext = viewModel;
    }
function showLoginPageForm(slow){
                var initialContainer = page.getViewById("initial-container");
                var mainContainer = page.getViewById("container");
                var containerLogo = page.getViewById("container-logo");
    }

在showLoginPageForm中,另一个局部函数到page.ts文件显示

error TS2304: Cannot find name 'page'

我不知道自己错过了什么

3 个答案:

答案 0 :(得分:1)

如果在本地函数中需要全局变量,则应考虑使用函数参数或基于类的对象。

基于类的解决方案(据我称之为解决方案)

class Navigator {

    protected page: pageModule.Page;
    protected viewModel: launchViewModelModule.LaunchViewModel;

    constructor(viewModel: launchViewModelModule.LaunchViewModel) {
        this.viewModel = viewModel;
    }

    public navigateTo(args: observableModule.EventData): void {

        this.page = args.object as pageModule.Page;
        this.page.bindingContext = viewModel;
    }

    public showLoginPageForm(slow) {

        const initialContainer = this.page.getViewById("initial-container");
        const mainContainer = this.page.getViewById("container");
        const containerLogo = this.page.getViewById("container-logo");
    }
}

答案 1 :(得分:0)

变量page被定义为函数navigatingTo中的局部变量,对函数showLoginPageForm不可见。

也许你想在函数之外声明page,以便它们都可以看到。

答案 2 :(得分:0)

在查看已经使用nativescript-typescript

完成的项目后,我找到了解决方案
var viewModel : launchViewModelModule.LaunchViewModel;
viewModel = new launchViewModelModule.LaunchViewModel();

let initialContainer: StackLayout;
let mainContainer: StackLayout;
let containerLogo: AbsoluteLayout;


export function navigatingTo(args: observableModule.EventData) {
    var page = <pageModule.Page>args.object;
    page.bindingContext = viewModel;

    initialContainer = <StackLayout>page.getViewById("initial-container");
    mainContainer = <StackLayout>page.getViewById("container");
    containerLogo = <AbsoluteLayout>page.getViewById("container-logo");
    runAnimation();    
}

然后从这里调用函数,例如showLoginPageForm(),可以访问属于您要使用的对象的所有属性和方法initialContainer.animate()。谢谢@Kirby