You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/casl-vue/README.md
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -133,6 +133,75 @@ In that way, you can also provide a different ability for a components subtree.
133
133
**Note**: I don't recommend to manage more than 1 `Ability` instance in the app
134
134
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).
135
135
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
+
importVuefrom'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
+
<canI="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
+
<CanI="read"a="Post">...</Can>
168
+
```
169
+
170
+
* use `of` alias instead of `a` when you check by subject field
171
+
172
+
```html
173
+
<CanI="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
+
<CanI="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
+
<CanI="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
+
<Cando="read":on="post">...</Can>
194
+
195
+
<!-- or per field check -->
196
+
<Cando="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
+
<CannotI="read"a="Post">...</Can>
203
+
```
204
+
136
205
## Want to help?
137
206
138
207
Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on guidelines for [contributing][contributing]
0 commit comments