【问题标题】:ion-label component inside the stencil-component-starter not getting renderedstencil-component-starter 中的 ion-label 组件未呈现
【发布时间】:2018-08-26 07:36:36
【问题描述】:

我从以下位置克隆了stencil-component-starter

https://github.com/ionic-team/stencil-component-starter

然后在文件上:my-component.tsx我有以下代码:

import { Component, Prop } from '@stencil/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true
})
export class MyComponent {

  @Prop() first: string;
  @Prop() last: string;
  @Prop() minHeartRate: number;
  @Prop() maxHeartRate: number;

  render() {
    return (
      <div>
        Athlete: {this.first} {this.last} - Heart Rate Range:
        <ion-range mode="ios" dualKnobs={true} min={0} max={200} step={2} pin={true} snaps={true} value={{lower: this.minHeartRate, upper: this.maxHeartRate}}>
          <ion-label range-left>0</ion-label>
          <ion-label range-right>200</ion-label>
        </ion-range>
      </div>
    );
  }
}

如您所见:

https://github.com/napolev/stencil-ion-range/blob/master/src/components/my-component/my-component.tsx

[之前]它正在几乎正确渲染,有两个问题1 and 2
[现在]它没有得到完全呈现。所以,有三个问题1, 2 and 3

  1. &lt;ion-label/&gt; 标签被忽略。

  2. 每个旋钮都可以移动到另一个旋钮之外(请忽略这一点,我认为这是新版本的故意)。

  3. 这是我刚刚检测到(Current time: 2018-08-26 19:20) 的一个新问题。就像 12 小时前(检查 repo 上的时间戳)存储库:https://github.com/napolev/stencil-ion-range/ on commit 12c14b75cca92f19af6c5ccae60091319578cec7 正在生成 几乎 上面的 &lt;ion-range/&gt; 标签,除了问题 1 和 2(检查下图) .但是现在在干净安装此存储库之后,它不再呈现您在下图中看到的内容。这很奇怪。完成该提交后,我检查了它是否在全新安装后呈现。

这是它之前渲染的内容,现在不再渲染:

参考:

https://ionicframework.com/docs/api/components/range/Range/

关于如何解决这个问题的任何想法?

谢谢!

[编辑 1]

这是对以下 Aaron Saunders 评论的回应:

ion-label component inside the stencil-component-starter not getting rendered

Aaron,我添加了您建议的代码,您可以在此处看到:

https://github.com/napolev/stencil-ion-range/commit/d3471825131d3d329901c73d8c6803a609b27c3b#diff-34c2a6c626ee2612cd4f12b2c004a0b1L16

但是当使用$ npm start 运行代码时,会呈现以下内容:

您是否正确渲染了组件?

我删除了node_modules 目录并再次安装它们,但没有成功。

你可以试试我的存储库吗?

如您所见,在官方提交的基础上,我只完成了 2 次提交:

https://github.com/napolev/stencil-ion-range/commits/master

以防万一这里是我用于主要工具的版本:

$ node -v
v8.11.2

$ npm -v
6.1.0

$ ionic -v
ionic CLI 4.1.1

[编辑 2]

关于这个话题有一个平行的讨论:

https://forum.ionicframework.com/t/ion-label-component-inside-the-stencil-component-starter-not-getting-rendered/139763

[编辑 3]

这是对以下 Alexander Staroselsky 评论的回应:

ion-label component inside the stencil-component-starter not getting rendered

Alexander,我尝试了您的建议并进行了以下更改:

https://github.com/napolev/stencil-ion-range/commit/68fce2abe25536b657f9493beb1cc56e26828e4f

现在&lt;ion-range/&gt; 组件被渲染(这非常好),但渲染时出现了一些问题,如下图所示。 &lt;ion-label/&gt; 组件的宽度很大。

知道如何解决这个问题吗?

谢谢!

[编辑 4]

添加 Aaron Saunders 的建议可以解决问题:

<ion-item>
    <ion-range
        mode="ios"
        dualKnobs={true}
        min={0} max={200} step={2}
        pin={true} snaps={true}
        value={{ lower: this.minHeartRate, upper: this.maxHeartRate }}
    >
        <ion-label slot="start" style={{flex: 'none', margin: '0 30px 0 0'}}>0</ion-label>
        <ion-label slot="end" style={{flex: 'none', padding:' 0 0 0 30px'}}>200</ion-label>
    </ion-range>
</ion-item>

感谢 Alexander StaroselskyAaron Saunders,因为结合他们的建议,我可以完成这项工作。

【问题讨论】:

  • 你要么导入'@ionic/core';在您的 stencil-component-starter 组件中或使用在您的index.html 中包含 CDN @ionic/core 资产?创建一个新的 stencil-component-starter 项目并导入/链接任何一个都允许使用和导出元素。肯定存在样式不一致,但这是与测试版相关的完全不同的问题。我不想正式回答,除非这显然解决了在启动项目中使用@ionic/core 组件的基本问题。

标签: typescript ionic-framework stenciljs


【解决方案1】:

您需要在stencil-component-starter 组件中显式地import '@ionic/core'; 或将CDN scripts/styles 添加到index.html。我记得有一次工具包或 stencil-app-starter 专门在具有早期版本的 @ionic/core beta 甚至 alpha 的根元素中进行了导入。

此外,根据documentation,您还需要用ion-item 包装ion-range,以及使用slot="start"slot="end" 而不是range-leftrange-right

import { Component, Prop } from '@stencil/core';
import '@ionic/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true
})
export class MyComponent {

  @Prop() first: string;
  @Prop() last: string;
  @Prop() minHeartRate: number;
  @Prop() maxHeartRate: number;

  render() {
    return (
      <div>
        Athlete: {this.first} {this.last} - Heart Rate Range:
        <ion-item>
          <ion-range mode="ios" dualKnobs={true} min={0} max={200} step={2} pin={true} snaps={true} value={{lower: this.minHeartRate, upper: this.maxHeartRate}}>
            <ion-label slot="start">0</ion-label>
            <ion-label slot="end">200</ion-label>
          </ion-range>
        </ion-item>
      </div>
    );
  }
}

这将有助于确保注入样式并正确呈现组件。平心而论,当我尝试这个时,大多数样式都通过了,但是开槽 ion-label 元素的定位肯定存在问题。肯定需要对样式进行一些调整,包括 flex grow/shrink/basis 以及 end slot 元素的定位/margin。在@ionic/core github 上提交样式问题可能是明智的。

希望对您有所帮助!

【讨论】:

  • Alexander,我在原帖中回复了 [EDIT 3]。谢谢!
  • Alexander,当我采用导入方式时:@ionic/core 在我的组件中一切正常,但编译后的组件有很多 JavaScript 文件,我认为其中很多是 @ionic/core 或 @ 987654336@源代码。想象一下我有 10 个自定义组件的情况,对于每个组件,我可能会有 10 倍的通用代码。我想知道是否有办法防止这种情况发生?可能是您上面建议的第二种方式:“将 CDN 脚本/样式添加到 index.html”实现此目的?我的意思是,以前的核心代码未包含在我的代码中,但我可以通过 CDN 使用它?谢谢!
  • 这个问题有更新吗?我正在使用npm init stencil 在新生成的组件中导入@ionic/core,并尝试使用ion-labelion-chip,但它仍然崩溃:Uncaught (in promise) Error: Constructor for "ion-label#undefined" was not found
【解决方案2】:

基于最新的 4.0 beta...我仍然认为默认情况下标签的样式存在错误,但这是一种解决方法

由于 stencil 使用 ionic v4 - https://beta.ionicframework.com/docs/api/range,因此对文档的引用

<ion-range mode="ios" 
           dualKnobs={true} 
           min={0} max={200} 
           step={2} pin={true} 
           snaps={true} 
           value={{lower: 89, upper: 150}}>
   <ion-label slot="start" style={{flex: 'none',padding:'10px'}}>0</ion-label>
   <ion-label slot="end" style={{flex: 'none',padding:'10px'}}>200</ion-label>
</ion-range>

【讨论】:

  • 亚伦,还是不行。有关更多详细信息,我在 [EDIT 1] 上的原始帖子中回答了您。谢谢!
  • 谢谢亚伦。最后,我通过将您的建议与上述 Alexander Staroselsky 的建议相结合来完成这项工作。
猜你喜欢
  • 1970-01-01
  • 2019-08-13
  • 2018-12-25
  • 2014-08-12
  • 2021-05-14
  • 2013-06-08
  • 2018-05-30
  • 2021-11-19
相关资源
最近更新 更多