6. 组件 -- Highway MVVM

本文介绍 Highway.js 中的组件定义与使用方法,包括全局与局部组件注册、父子组件间引用与通信机制等核心概念。

6-1. 自定义

通过Highway.extend抽取为类,需指定标签名

6-1-1. 全局

通过Highway.component指定

6-1-2. 局部

通过View.$components指定

6-1-3. 示例

<!-- examples/components/customized.html -->

<div id="app">
  <p>i am {{name}}</p>
  <my-component hi-ref="component" class="component"></my-component>
</div>

<script id="t-my-component" type="text/template">
  <p>i am {{name}}</p>
</script>

const myComponent = Highway.extend({
  $template: $('#t-my-component').html(),
  $scope: {
    name: 'component'
  }
});

// 全局
//Highway.component('my-component', myComponent);

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'app'
  },
  // 局部
  $components: {
    'my-component': myComponent
  }
});

6-2. 引用

6-2-1. 子视图

父视图通过$refs[‘name’]获取子视图上下文,子视图需指定hi-ref指令

<!-- examples/components/ref.html -->

<div id="app">
  <p>i am {{name}}</p>
  <my-component hi-ref="component" class="component"></my-component>
  <button hi-on:click="getComponentName">getComponentName</button>
  <button hi-on:click="triggerComponentHandler">triggerComponentHandler</button>
</div>

<script id="t-my-component" type="text/template">
  <p>i am {{name}}, my parent is {{$parent.name}}</p>
</script>

const myComponent = Highway.extend({
  $template: $('#t-my-component').html(),
  $scope: {
    name: 'component'
  },
  introduce() {
    alert('hi, i am component');
  }
});

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'app'
  },
  $components: {
    'my-component': myComponent
  },
  getComponentName() {
    const $component = this.$components.$refs['component'];

    alert($component.$get('name'));
  },
  triggerComponentHandler() {
    const $component = this.$components.$refs['component'];
    $component.introduce();
  }
});

6-2-2. 父视图

子视图通过$parent获得父视图上下文

<!-- examples/components/parent.html -->

<div id="app">
  <p>i am {{name}}</p>
  <my-component class="component"></my-component>
</div>

<script id="t-my-component" type="text/template">
  <p>i am {{name}}, my parent is {{$parent.name}}</p>
  <button hi-on:click="getParentName">getParentName</button>
  <button hi-on:click="triggerParentHandler">triggerParentHandler</button>
</script>

const myComponent = Highway.extend({
  $template: $('#t-my-component').html(),
  $scope: {
    name: 'component'
  },
  getParentName() {
    alert(this.$parent.$get('name'));
  },
  triggerParentHandler() {
    this.$parent.introduce();
  }
});

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'app'
  },
  $components: {
    'my-component': myComponent
  },
  introduce() {
    alert('hi, i am app');
  }
});

6-3. 通信

6-3-1. $on

监听事件,需指定事件名与处理函数,返回停止监听

<!-- examples/components/event.html -->

this.$on('app:event', this.eventHandler.bind(this));

// stop listening if stopper is called
//stopper()

6-3-2. $off

关闭监听,需指定事件名与处理函数,如处理函数不指定,关闭在该事件上的所有处理器

<!-- examples/components/event.html -->

this.$off('app:event');

6-3-2. $broadcast

广播事件,向子视图发送通知消息

<!-- examples/components/event.html -->

this.$broadcast('app:event', 'this is event from app');

6-3-3. $emit

冒泡事件,向父视图发送通知消息

<!-- examples/components/event.html -->

this.$emit('component:event', 'this is event from component');

6-3-4. $fire

触发自身事件,并不会触发 broadcast emit

<!-- examples/components/event.html -->

this.$fire('component:self', 'this is event from self');

6-3-5. $listenTo

监听指定对象事件,指定对象通过$fire触发事件

<!-- examples/components/listen.html -->

const $component = this.$components.$refs['component'];
const stopper = this.$listenTo($component, 'component:event', this.eventHandler.bind(this));

// stop listening if stopper is called
//stopper()      

6-3-6. $listenToOnce

监听一次指定对象事件,指定对象通过$fire触发事件

<!-- examples/components/listen.html -->

const stopper = this.$listenToOnce($component, 'component:event', this.eventHandler.bind(this));

// stop listening if stopper is called
//stopper()

6-3-7. $stopListening

取消监听指定对象事件

<!-- examples/components/listen.html -->

const $component = this.$components.$refs['component'];
this.$stopListening($component, 'component:event');

6-4. 作用域链

视图与子视图作用域(不同于上下文)相互隔离,可通过$parent中获取作用域

<!-- examples/components/scope.html -->

<div id="app">
  <p>i am {{name}}</p>
  <my-component class="component"></my-component>
</div>

<script id="t-my-component" type="text/template">
  <p>i am {{name}}, my parent is {{$parent.name}}</p>
</script>

name属性值只会从当前上下文作用域获取值,如遇空值并不会自动向父组件寻找,如需向上寻找添加$parent前缀。

参考如下name取值

view- childview-parenthi, i am {{name}}, my parent is {{$parent.name}}
’ child’‘parent’hi, i am child, my parent is parent
undefined‘parent’hi, i am , my parent is parent
‘child’undefinedhi, i am child, my parent is
undefinedundefinedhi, i am , my parent is

repeat指令会产生临时作用域

(.venv) PS C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Scripts> pip install gluonnlp Collecting gluonnlp Using cached gluonnlp-0.10.0.tar.gz (344 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: numpy>=1.16.0 in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (1.26.4) Collecting cython (from gluonnlp) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl.metadata (6.0 kB) Requirement already satisfied: packaging in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (24.1) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl (2.7 MB) Building wheels for collected packages: gluonnlp DEPRECATION: Building 'gluonnlp' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possib le replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'gluonnlp'. Discussion can be found at https://github.com/pypa/pip/issues/6334 Building wheel for gluonnlp (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [136 lines of output] C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Lib\site-packages\setuptools\__init__.py:92: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated. !! ******************************************************************************** Requirements should be satisfied by a PEP 517 installer. If you are using pip, you can try `pip install --use-pep517`. By 2025-Oct-31, you need to update your project and remove deprecated calls or your builds will no longer be supported. ******************************************************************************** !! dist.fetch_build_eggs(dist.setup_requires) running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\base.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\_constants.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp creating build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\collector.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration creating build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\baidu_ernie_data.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\candidate_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\classification.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\conll.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataset.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\datasetloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\intent_slot.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\question_answering.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\registry.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sentiment.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\stream.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\super_glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\transforms.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\word_embedding_evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data creating build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\token_embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding creating build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\initializer.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer creating build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\activation_regularizer.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\label_smoothing.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss creating build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\length_normalized_loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\masked_accuracy.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric creating build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\attention_cell.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bilm_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\convolutional_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\highway.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\info.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\lstmpcellwithclip.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sampled_block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\seq2seq_encoder_decoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sequence_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\transformer.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model creating build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\bert_adam.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer creating build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\files.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parallel.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\seed.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\version.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils creating build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\subwords.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\vocab.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab creating build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\batchify.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify creating build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert creating build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\google_billion_word.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\large_text_compression_benchmark.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\wikitext.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora creating build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet creating build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\cache.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train running egg_info writing src\gluonnlp.egg-info\PKG-INFO writing dependency_links to src\gluonnlp.egg-info\dependency_links.txt writing requirements to src\gluonnlp.egg-info\requires.txt writing top-level names to src\gluonnlp.egg-info\top_level.txt reading manifest file 'src\gluonnlp.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.py' under directory 'gluonnlp' warning: no previously-included files matching '*' found under directory 'tests' warning: no previously-included files matching '*' found under directory 'scripts' adding license file 'LICENSE' writing manifest file 'src\gluonnlp.egg-info\SOURCES.txt' copying src\gluonnlp\data\fast_bert_tokenizer.c -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\fast_bert_tokenizer.pyx -> build\lib.win-amd64-cpython-312\gluonnlp\data running build_ext Compiling src/gluonnlp/data/fast_bert_tokenizer.pyx because it changed. [1/1] Cythonizing src/gluonnlp/data/fast_bert_tokenizer.pyx building 'gluonnlp.data.fast_bert_tokenizer' extension error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for gluonnlp Running setup.py clean for gluonnlp Failed to build gluonnlp ERROR: Failed to build installable wheels for some pyproject.toml based projects (gluonnlp) 以上为安装gluonnlp的报错结果
06-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值