Skip to content

Commit

Permalink
feat: support js use
Browse files Browse the repository at this point in the history
  • Loading branch information
doxiaodong committed Jun 27, 2018
1 parent 6a51a22 commit 1ac4494
Show file tree
Hide file tree
Showing 13 changed files with 5,912 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
coverage/
lib/
node_modules/
pages/.umi/
8 changes: 0 additions & 8 deletions .npmignore

This file was deleted.

69 changes: 65 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

## Usage

* `npm i mobx-di`
- `npm i mobx-di`

* Basic store User
- Basic store User

```typescript
import { observable, action, runInAction } from 'mobx'
Expand All @@ -34,7 +34,9 @@ export class User {
}
```

* Another store which has the `User` dependence
- Another store which has the `User` dependence

typescript

```typescript
import { computed, action } from 'mobx'
Expand Down Expand Up @@ -67,7 +69,42 @@ export class HomeStore {
}
```

* Inject in component
javascript

```javascript
import { computed, action } from 'mobx'
import { Injectable, Inject } from 'mobx-di'

import { User } from '../stores/user'

@Injectable(User)
export class HomeStore {
constructor(_user) {
console.log('user', _user)
}
@computed
get userInfo() {
return this._user.userInfo
}

// or
// @Inject(User) _user

@action.bound
toggle() {
return this._user.logout()
}

@computed
get btnDesc() {
return this.userInfo.isLogin ? '退出' : '登录'
}
}
```

- Inject in component

typescript

```typescript
import * as React from 'react'
Expand All @@ -91,6 +128,30 @@ export default class Home extends React.Component {
}
```

javascript

```javascript
import * as React from 'react'
import { observer } from 'mobx-react'
import { Inject } from 'mobx-di'

import { HomeStore } from './store'

@observer
export default class Home extends React.Component {
@Inject(HomeStore) home

render() {
return (
<>
<button onClick={this.home.toggle}>{this.home.btnDesc}</button>
{JSON.stringify(this.home.userInfo)}
</>
)
}
}
```

## Advance

1. You can replace any store in your code by `replace` mothod if you want,
Expand Down
Loading

0 comments on commit 1ac4494

Please sign in to comment.