stenciljs手表装饰器不触发

时间:2018-06-22 09:46:22

标签: javascript web-component stenciljs

我正在尝试将JSON字符串传递给组件属性,然后让组件将字符串解析为javascript对象。

不幸的是, @Watch装饰器没有触发 ...我做错了什么?我遵循了以下文档:https://stenciljs.com/docs/reactive-data#watch-decorator

这是我代码的相关部分:

ptw-input.tsx

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

    @Component({
      tag: "ptw-input",
      styleUrl: "ptw-input.css",
      shadow: true
    })
    export class PtwInput {

      @Prop() data: string;

      innerData: any;

      @Watch('data')
      watchHandler(newValue: string): void {
        this.innerData = JSON.parse(newValue);
        console.log('this.innerData', this.innerData);
      }

      render(): JSX.Element {
        return (
          <input type="text" 
                 placeholder={this.innerData.placeholder}
                 disabled={this.innerData.disabled}
                 maxlength={this.innerData.maxlength}
        );
      }
    }

index.html                  

    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">
      <title>Stencil Component Starter</title>
      <script src="/build/ptw.js"></script>
    </head>

    <body>
        <ptw-input data='{"placeholder":"Type something", "disabled":false, "maxlength":4}'></ptw-input>
    </body>        
    </html>

...这是我的 package.json

    {
      "name": "ptw-input",
      "version": "0.0.1",
      "description": "PTW input component",
      "module": "dist/esm/index.js",
      "main": "dist/index.js",
      "types": "dist/types/components.d.ts",
      "collection": "dist/collection/collection-manifest.json",
      "files": [
        "dist/"
      ],
      "scripts": {
        "build": "stencil build",
        "dev": "sd concurrent \"stencil build --dev --watch\" \"stencil-dev-server\" ",
        "serve": "stencil-dev-server",
        "start": "npm run dev",
        "test": "jest",
        "test.watch": "jest --watch"
      },
      "dependencies": {},
      "devDependencies": {
        "@stencil/core": "^0.9.11",
        "@stencil/dev-server": "latest",
        "@stencil/utils": "latest",
        "@types/jest": "^21.1.1",
        "jest": "^21.2.1"
      },
      "repository": {
        "type": "git",
        "url": "git+https://github.com/ionic-team/stencil-component-starter.git"
      },
      "author": "Ionic Team",
      "license": "MIT",
      "bugs": {
        "url": "https://github.com/ionic-team/stencil"
      },
      "homepage": "https://github.com/ionic-team/stencil",
      "jest": {
        "transform": {
          "^.+\\.(ts|tsx)$": "<rootDir>/node_modules/@stencil/core/testing/jest.preprocessor.js"
        },
        "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
        "moduleFileExtensions": [
          "ts",
          "tsx",
          "js",
          "json",
          "jsx"
        ]
      }
    }

我正在使用package.json中显示的最新stenciljs,但控制台日志中除了以下错误外没有任何内容:TypeError: Cannot read property 'placeholder' of undefined

1 个答案:

答案 0 :(得分:4)

最初加载组件时,@Watch装饰器不会触发。

要使该方法在组件加载时运行,请在componentWillLoad lifecycle hook内部调用该方法:

componentWillLoad() {
 this.watchHandler(this.newValue);
}