【问题标题】:ionic2 angular2 add graph component (with its own selector) to a pageionic2 angular2 将图形组件(带有自己的选择器)添加到页面
【发布时间】:2016-10-26 22:16:31
【问题描述】:

Ionic2 / Angular2中:我试图弄清楚如何将带有自己的选择器的图形添加到页面中。

从 Ionic2 教程项目开始,我添加了 2 个元素:“MyPage”和“MyGraphDiagram”。我想在“MyPage”中使用“MyGraphDiagram”。

在“[MyProject]/src/app/app.module.ts”我有:

import ...
import { MyPage } from '../pages/my/my';
import { MyGraphDiagram } from '../pages/my/graph-components/my-graph';
@NgModule({
  declarations: [
    MyApp,
    HelloIonicPage,
    ItemDetailsPage,
    ListPage,
    MyPage,
    MyGraphDiagram
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HelloIonicPage,
    ItemDetailsPage,
    ListPage,
    MyPage,
    MyGraphDiagram
  ],
  providers: []
})
export class AppModule {}

FIRST:在这里,如果我编译项目,则声明节点中的 MyGraphDiagram 存在问题(我还认为我已经正确实现了 MyGraphDiagram)。

在 [MyProject]/src/app/app.component.ts 中,它保持不变,只是将根页面替换为:rootPage: any=MyPage

在“[MyProject]/src/pages/my/graph-components/my-graph.ts”中(这是thread的副本):

import {Component, View, Input, ViewChild, ElementRef} from 'angular2/core';

@Component({
    selector: 'my-graph',
})
@View({
    template: `<canvas #myGraph class='myGraph'
     [attr.width]='_size'
     [attr.height]='_size'></canvas>`,
})

export class MyGraphDiagram {
    private _size: number;

    // get the element with the #myGraph on it
    @ViewChild("myGraph") myGraph: ElementRef; 

    constructor(){
        this._size = 150;
    }

    ngAfterViewInit() { // wait for the view to init before using the element

      let context: CanvasRenderingContext2D = this.myGraph.nativeElement.getContext("2d");
      // happy drawing from here on
      context.fillStyle = 'blue';
      context.fillRect(10, 10, 150, 150);
    }

    get size(){
        return this._size;
    }

    @Input () set size(newValue: number){
        this._size = Math.floor(newValue);
    }
}

然后最后在“[MyProject]/src/pages/my/my.ts”下:

import {Component} from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

import { MyGraphDiagram } from '/graph-component/my-graph'


@Component({
    selector:'my-page',
    template: 'my.html'
})
export class MyPage {
    constructor(public navCtrl: NavController, public navParams: NavParams){

    }
}

还有“[MyProject]/src/pages/my/my.html”:

<ion-header>...
</ion-header>

<ion-content>
  <my-graph></my-graph>
</ion-content>

如果我运行 CLI “ionic run android”: 如果我将 MyGraphDiagram 留在“[MyProject]/src/app/app.module.ts”的 declaration 节点中,则 lint 部分不会通过。它抛出一个错误:

错误:模块声明的意外值“MyGraphDiagram” '应用模块'

如果我采用 declartion 节点的 MyGraphDiagram,lint 部分会通过,但构建会抛出该错误:

'my-graph' 不是已知元素:

[00:08:06] 1. 如果 'my-graph' 是 Angular 组件,则验证 它是这个模块的一部分。 [00:08:06] 2. 如果 'my-graph' 是 Web 组件然后将“CUSTOM_ELEMENTS_SCHEMA”添加到 '@NgModule.schema'

一些更新: 如果我运行 CLI“离子服务”:

TypeScript Transpile 失败并出现以下错误:

找不到名称“ElementRef”
L13: myGraph: ElementRef;

【问题讨论】:

    标签: canvas angular ionic2


    【解决方案1】:

    首先,从 entryComponents 中移除 MyGraphDiagram 并将其保留在 app.module.ts 中的声明中。

    然后,更新 MyGraphDiagram 组件以遵循 RC1 标准,使用 html 模板并删除 @view 部分。

    【讨论】:

    • 我做了:MyGraphDiagramm 从入口组件中删除。然后在 "[MyProject]/src/pages/my/graph-components/my-graph.ts" @Component 更改为:@Component({ selector: 'my-graph', templateUrl: 'my-graph.html' } )` 新文件:“[MyProject]/src/pages/my/graph-components/my-graph.html”与:&lt;ion-header&gt; &lt;/ion-header&gt; &lt;ion-content padding&gt; &lt;canvas #myGraph class='myGraph' [attr.width]='_size' [attr.height]='_size'&gt;&lt;/canvas&gt; &lt;/ion-content&gt;。在 CLI Ionic serve 和 TSC --watch 中运行时,我仍然得到:“它找不到名称 'ElementRef' L13: myGraph: ElementRef;”
    • 我明白了。现在,您可以将视图中的类型删除为:@ViewChild("myGraph") myGraph;
    • 我安装了一个 npm (似乎是 ionic2 中的魔术),我收到的错误消息更少。页面文件夹中的子文件夹似乎也有一些问题。项目显示,但事件 ngAfterViewInit() 未触发,因此屏幕上仍然没有可见形状。
    • 一些用户评论说删除 Ionic 并重新安装它解决了一些与类型相关的问题,特别是如果项目已从以前的 Ionic 版本升级。
    • 我会用我的进度挖掘和更新我的问题。谢谢你的提示。如果你熟悉它,它会用 symfony 和“清理缓存”或“资产重建”来记住我。
    【解决方案2】:

    您使用哪个版本的 ionic? @view 已弃用,您可以定义您的组件,例如: @Component({ selector: 'my-graph', template: `<canvas #myGraph class='myGraph' [attr.width]='_size' [attr.height]='_size'></canvas>` }) export class MyGraphDiagram { ...

    【讨论】:

    • 在“[MyProject]/src/pages/my/graph-components/my-graph.ts”中,我在@Component注解中取了模板并取走了\\@view注解(+从导入文件顶部的语句以使其:“从'angular2 / core'导入{Component,Input,ViewChild,ElementRef};”。我仍然有错误消息:“在构建错误:意外的值'MyGraphDiagram'声明模块“AppModule””
    • 为了完成你的问题,我使用 Ionic 2.1.0。
    • 感谢您的帮助,问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多