Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

组织自己的store可以使用这种方法 #14

Open
zwmmm opened this issue Sep 28, 2018 · 5 comments
Open

组织自己的store可以使用这种方法 #14

zwmmm opened this issue Sep 28, 2018 · 5 comments

Comments

@zwmmm
Copy link
Contributor

zwmmm commented Sep 28, 2018

a.js

export default  {
    data: {
        numa: 1,
        numb: 2
    },
    getA() {
        console.log('a');
    }
}

b.js

export default  {
    data: {
        numc: 3,
        numd: 4
    },
    getA() {
        console.log('b');
    }
}

store.js

import a from './a.js'
import b from './b.js'

const commonData = {
    commona: 'common'
}
function storeMixin(options) {
    let result = {
        data: commonData ,
    }
    for (let k in options) {
        let value = options[k];
        if (value.data) {
            result.data[k] = value.data
            delete value.data
        }
        Object.assign(result, value)
    }
    return result;
}

export default storeMixin({a, b})

根据page划分store, 不过函数名字就不能重复了

@zwmmm
Copy link
Contributor Author

zwmmm commented Sep 28, 2018

最终返回的值是

{
    data: {
        a: {
            numa: 1,
            numb: 2
        },
        b: {
            numc: 3,
            numd: 4
        }
    },
    getA() {
        console.log('a');
    },
    getB() {
        console.log('b');
    }
}

@dntzhang
Copy link
Collaborator

赞,不错~

@bili-jing
Copy link

update的时候key应该怎么样呢
image
请问如何更新到roomStore里面的数据呢.
image
然后wxml里面该如何绑定数据。我试了roomStore.xxx不行.直接xxx也不行呢.
image

@zwmmm
Copy link
Contributor Author

zwmmm commented Aug 5, 2019

update的时候key应该怎么样呢
image
请问如何更新到roomStore里面的数据呢.
image
然后wxml里面该如何绑定数据。我试了roomStore.xxx不行.直接xxx也不行呢.
image

仔细看下readme
第一个问题 数据更新 使用

this.store.data.a = 'a'
this.update()

在wxml中使用的数据需要现在page的data中声明(但是默认值要在store中设置)

{
  data: {
     a: null
  },
  onload() {
    this.store.data.a = 'a'
    this.update()
 }
}
<div>{{ a }}</div>

@bili-jing
Copy link

@zwmmm 谢谢老哥。情况是
{
data: {
a: {
numa: 1,
numb: 2
},
b: {
numc: 3,
numd: 4
}
},
getA() {
console.log('a');
},
getB() {
console.log('b');
}
}
a-page对应的data也应该是
a: {
numa: 1,
numb: 2
},
是么.
然后wxml绑定即{{a.numa1}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants