Skip to content

Commit 641cb1a

Browse files
committed
docs(README): adds info about vue Can component
1 parent 2797ebb commit 641cb1a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

packages/casl-vue/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,75 @@ In that way, you can also provide a different ability for a components subtree.
133133
**Note**: I don't recommend to manage more than 1 `Ability` instance in the app
134134
because this contradicts to the main goal of CASL: keep all permissions in a single place and manage them from the single location (`Ability` instance).
135135

136+
### 5. `Can` component
137+
138+
There is an alternative way how you can check your permissions in the app, by using `Can` component. To enable this feature you need to register it globally:
139+
140+
```js
141+
import Vue from 'vue'
142+
import { Can } from '@casl/vue'
143+
144+
Vue.component('Can', Can)
145+
```
146+
147+
Now, instead of using `v-if="$can(...)"`, we can do this:
148+
149+
```html
150+
<template>
151+
<can I="create" a="Post">
152+
<a @click="createPost">Add Post</a>
153+
</can>
154+
</template>
155+
```
156+
157+
#### Property names and aliases
158+
159+
As you can see from the code above, component name and its property names and values creates an English sentence, basically a question.
160+
For example, the code above reads as `Can I create a Post`.
161+
162+
There are several other property aliases which allow to construct a readable question:
163+
164+
* use `a` alias when you check by Type
165+
166+
```html
167+
<Can I="read" a="Post">...</Can>
168+
```
169+
170+
* use `of` alias instead of `a` when you check by subject field
171+
172+
```html
173+
<Can I="read title" of="Post">...</Can>
174+
175+
<!-- or when checking on instance. `post` is an instance of `Post` class (i.e., model instance) -->
176+
177+
<Can I="read title" :of="post">...</Can>
178+
```
179+
180+
* use `this` alias instead of `of` and `a` when you check action on instance
181+
182+
```html
183+
<!-- `post` is an instance of `Post` class (i.e., model instance) -->
184+
185+
<Can I="read" :this="post">...</Can>
186+
```
187+
188+
* use `do` and `on` if you are bored and don't want to make your code more readable :)
189+
190+
```html
191+
<!-- `post` is an instance of `Post` class (i.e., model instance) -->
192+
193+
<Can do="read" :on="post">...</Can>
194+
195+
<!-- or per field check -->
196+
<Can do="read title" :on="post">...</Can>
197+
```
198+
199+
* use `not` when you want to invert the render method (renders children only if user can't read a post)
200+
201+
```html
202+
<Can not I="read" a="Post">...</Can>
203+
```
204+
136205
## Want to help?
137206

138207
Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on guidelines for [contributing][contributing]

0 commit comments

Comments
 (0)