1

所以,伙计们,我在这里有点噩梦。到目前为止开始使用 makoJs 和 Marko-widgets。但...

这是我按照一些示例的组织并与 lassoJS 集成的东西。所以在套索的做事方式中,我没有实现定义组件的方法。

所以我需要完成的事情:

module.exports = require('marko-widgets').defineComponent({
    template: require('./template.marko'),

    getInitialState: function(input) {
        return {
            name: input.name,
            selected: input.selected || false;
        }
    },

    getTemplateData: function(state, input) {
        var style = ;

        return {
            name: state.name,
            color: state.selected ? 'yellow' : 'transparent'
        };
    },

    handleClick: function() {
        this.setState('selected', true);
    },

    isSelected: function() {
        return this.state.selected;
    }
});

我的 Marko 文件是这样写的:

import './style.css'

static function getClassNameForValue(value) {
    if (value < 0) {
        return 'negative';
    } else if (value > 0) {
        return 'positive';
    }
}

class {
    onInput(input) {
        var value = input.value || 0;

        this.state = {
            value: value
        };
    }

    handleIncrementClick(delta) {

        this.state.value += delta;
        var value = this.state.value;


        var send = {
            value:value
        }
        console.log(value);
        $.post( "/tryPost", send,function( data ) {
            console.log(data);
        }); 
    }

    handleInputKeyUp(event, el) {
        var newValue = el.value;
        if (/^-?[0-9]+$/.test(newValue)) {
            this.state.value = parseInt(newValue, 10);
        }
    }
}

$ var value=state.value;

<div class=['number-spinner', getClassNameForValue(value)]>

    <button type="button" onClick("handleIncrementClick", -1)>
        -
    </button>

    <input type="text" value=state.value size="4"
        onKeyUp("handleInputKeyUp")>

    <button type="button" onClick("handleIncrementClick", 1)>
        +
    </button>

</div>

编辑:所以我在想,知道我正在使用的那些模块的哪个版本可以提供帮助。

    "lasso": "^2.11.3",
    "lasso-less": "^2.4.3",
    "lasso-marko": "^2.2.2",
    "lodash": "^4.17.4",
    "markdown-js": "0.0.3",
    "marked": "^0.3.6",
    "marko": "^4.0.0-rc.18",
    "marko-widgets": "^6.6.0"

那么,如何实现生命周期事件的监听器呢?

4

2 回答 2

1

A small detail, but it has to do with events, so maybe:

<button type="button" onClick("handleIncrementClick", -1)>

I think the event is named on-click

于 2018-11-09T01:42:33.283 回答
0

我不太确定问题是什么,但是对于markov4,marko-widgets不应该再安装(只marko应该安装)。您需要执行以下操作:

npm uninstall marko-widgets --save

We are still in the process of finalizing the v4 documentation, but maybe the following v4 docs on component lifecycle methods would help: https://github.com/marko-js/marko/blob/9b6a288d83fef926590f24368a40476c5ff0e240/docs/06-components.md#lifecycle

If things are still unclear, please join in the Gitter chat room: https://gitter.im/marko-js/marko

于 2017-02-25T18:15:16.260 回答