首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用web组件标记绑定嵌套的剔除组件?

如何使用web组件标记绑定嵌套的剔除组件?
EN

Stack Overflow用户
提问于 2016-04-26 18:06:57
回答 2查看 864关注 0票数 0

我真的很想使用嵌套的web-component样式标记,但我似乎永远无法让它工作起来。我让它工作是因为音符这里(在答案中)

问题在于在使用"web组件“语法时如何将params传递到组件中。

我不太明白他们在说什么,OP正在处理数组。据我所知,它从事物中产生计算的可观测值。但是,在我的嵌套组件构造函数中,params始终是$raw,什么都没有。

代码语言:javascript
运行
复制
<parent-component>
  <content>
    <!--works-->
    <div data-bind="component: { name: 'child-component', params: { id: $parent.id } }"></div>

    <!-- does not work -->
    <child-component params="id: '{{$parent.id}}'"></child-component>

  </content>
</parent-component>

jsFiddle

为什么我不能嵌套web-component风格的剔除组件并进行绑定工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-27 15:57:48

好吧,感谢@Jeroen让我提供了一个更好的例子。

我的问题是,我用单引号包装参数,并使用{{handle bars}}进行绑定。一旦我把那些拿掉,它看起来很好用。

代码语言:javascript
运行
复制
    <!-- does not work -->
    <child-component params="id: '{{$parent.id}}'"></child-component>

    <!-- wow, this does work! -->
    <child-component params="id: $parent.id"></child-component>
票数 0
EN

Stack Overflow用户

发布于 2016-04-27 08:47:16

我在这里有一个web组件的例子,https://github.com/brianlmerritt/knockout-babel-browserify-gulp

具有组件的典型主html:

代码语言:javascript
运行
复制
    <div class="row">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h2>Three Component View Model Binding Test</h2>
            </div>
            <div class="panel-body">
                <!-- THE GOAL OF THE PROJECT IS TO BIND EVERYTHING BEFORE AND AFTER THE FOLLOWING COMPONENT WITH centralViewModel -->
                <!-- ###ko stopBinding: true -->
                <div class="col-md-4">
                    <component-one></component-one>
                </div>
                <div class="col-md-4">
                    <component-two></component-two>
                </div>
                <div class="col-md-4">
                    <component-three></component-three>
                </div>
                <!-- ###/ko -->

                <p>Note: if component one did not load above, then check your browser console for errors</p>
            </div>

一个典型的组件--注意它是用ES2015 ()编写的

代码语言:javascript
运行
复制
const ko = require('knockout')
        , CentralData = require('../../service-providers/central-data')
        , CentralState = require('../../service-providers/central-state')
        , template = require('./template.html');

    const viewModel = function (data) {

        //Make service providers accessible to data-bind and templates
        this.CentralData = CentralData;
        this.CentralState = CentralState;

        this.componentName = 'Component One';
        this.foo = ko.observable(`${this.componentName} Foo`);
        this.bar = ko.observableArray(this.componentName.split(' '));
        this.barValue = ko.observable("");
        this.bar.push('bar');
        this.addToBar = (stuffForBar) => {
            if(this.barValue().length >= 1) {
                this.bar.push(this.barValue());
                CentralData.pushMoreData({firstName: this.componentName,secondName:this.barValue()});
            }
        };
        this.CentralState.signIn(this.componentName);
        if (CentralData.dataWeRetrieved().length < 10) {
            var dataToPush = {firstName : this.componentName, secondName : 'Foo-Bar'};
            CentralData.pushMoreData(dataToPush);
        }
    };
    console.info('Component One Running');
    module.exports = {
        name: 'component-one',
        viewModel: viewModel,
        template: template
    };

template.html:

代码语言:javascript
运行
复制
    <div>
        <h1 data-bind="text: componentName"></h1>
        <p>Foo is currently: <span data-bind="text: foo"></span></p>
        <p>Bar is an array. It's values currently are:</p>
        <ul data-bind="foreach: bar">
            <li data-bind="text: $data"></li>
        </ul>
        <form data-bind="submit: addToBar">
            <input type="text"
                   name="bar"
                   placeholder="Be witty!"
                   data-bind="attr: {id : componentName}, value : barValue" />
            <button type="submit">Add A Bar</button>
        </form>
        <h2>Central State</h2>
        <p>The following components are currently signed in to Central State Service Provider</p>
        <ul data-bind="foreach: CentralState.signedInComponents()">
            <li data-bind="text: $data"></li>
        </ul>
        <h2>Central Data</h2>
        <p>The following information is available from Central Data Service Provider</p>
        <table class="table table-bordered table-responsive table-hover">
            <tr>
                <th>Component Name</th><th>Second Value</th>
            </tr>
            <!-- ko foreach: CentralData.dataWeRetrieved -->
            <tr>
                <td data-bind="text: firstName"></td><td data-bind="text: secondName"></td>
            </tr>
            <!-- /ko -->
        </table>
        <h3>End of Component One!</h3>
    </div>

app.js具有组件绑定

代码语言:javascript
运行
复制
const ko = require('knockout');

/**
 * The modules that follow are singleton modules that do not appear to conflict with bindings
 *
 */

const CentralData = require('./service-providers/central-data');   // Mock centralised data/model service
const CentralState = require('./service-providers/central-state'); // Mock centralised state service

/**
 * Register knockout web components - these work, but conflict with any central bindings
 */

ko.components.register(
    'component-one',
    require('./components/component-one/component.js')
);

ko.components.register(
    'component-two',
    require('./components/component-two/component.js')
);

ko.components.register(
    'component-three',
    require('./components/component-three/component.js')
);

/**
 * This code comes from http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html
 *
 * Telling Knockout to Skip Binding Part of a Page
 *
 * However, when used around a component, the component does not load/run!
 *
 */

ko.bindingHandlers.stopBinding = {
    init: function() {
        return { controlsDescendantBindings: true };
    }
};

ko.virtualElements.allowedBindings.stopBinding = true;


/**
 * centralViewModel should be loadable for "all of the page not managed by components"
 *
 * That is the goal of the project, which I am failing in badly at the moment
 *
 */

const centralViewModel = {
    bindingWorked : ko.observable('The binding worked. KO view model centralViewModel has a this context and was bound to the html correctly')
};

console.info('app.js applying bindings...');

/**
 * The next line should bind central viewmodel to everything, but conflicts with components!!
 */

ko.applyBindings(centralViewModel,document.body);

// ko.applyBindings(); // Pull in all of the components


/**
Note: to avoid binding issues, we should be able to add these html comments around each component
 <!-- ko stopBinding: true -->
 <component-name>
 <!-- /ko -->
 However - when we do, the components no longer work
 */
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36872569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档