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: i18n/en/docusaurus-plugin-content-docs/current/guides/tech/with-nuxtjs.mdx
+72-10Lines changed: 72 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ sidebar_position: 10
3
3
---
4
4
# Usage with NuxtJS
5
5
6
-
It is possible to implement FSD in a NuxtJS project, but conflicts arise due to differences between the NuxtJS project structure requirements and FSD principles:
6
+
It is possible to implement FSD in a NuxtJS project, but conflicts arise due to the differences between NuxtJS project structure requirements and FSD principles:
7
7
8
8
- Initially, NuxtJS offers a project file structure without a `src` folder, i.e. in the root of the project.
9
9
- The file routing is in the `pages` folder, while in FSD this folder is reserved for the flat slice structure.
@@ -12,32 +12,92 @@ It is possible to implement FSD in a NuxtJS project, but conflicts arise due to
12
12
## Adding an alias for the `src` directory
13
13
14
14
Add an `alias` object to your config:
15
-
16
15
```ts title="nuxt.config.ts"
17
16
exportdefaultdefineNuxtConfig({
18
-
devtools: { enabled: true }, // Not related to FSD, enabled at project startup
17
+
devtools: { enabled: true }, // Not FSD related, enabled at project startup
19
18
alias: {
20
19
"@": '../src'
21
20
},
22
21
})
23
22
```
23
+
## Choose how to configure the router
24
+
25
+
In NuxtJS, there are two ways to customize the routing - using a config and using a file structure.
26
+
In the case of file-based routing, you will create index.vue files in folders inside the app/routes directory, and in the case of configure, you will configure the routers in the `router.options.ts` file.
27
+
28
+
29
+
### Routing using config
30
+
31
+
In the `app` layer, create a `router.options.ts` file, and export a config object from it:
32
+
```ts title="app/router.options.ts"
33
+
importtype { RouterConfig } from'@nuxt/schema';
34
+
35
+
exportdefault <RouterConfig> {
36
+
routes: (_routes) => [],
37
+
};
38
+
39
+
```
40
+
41
+
To add a `Home` page to your project, you need to do the following steps:
42
+
- Add a page slice inside the `pages` layer
43
+
- Add the appropriate route to the `app/router.config.ts` config
44
+
45
+
46
+
To create a page slice, let's use the [CLI](https://github.com/feature-sliced/cli):
47
+
48
+
```shell
49
+
fsd pages home
50
+
```
24
51
25
-
## Move file routing to `src/app`
52
+
Create a ``home-page.vue`` file inside the ui segment, access it using the Public API
First of all, create a `src` directory in the root of the project, and create app and pages layers inside this directory and a routes folder inside the app layer.
First of all, create a `src` directory in the root of your project, and create app and pages layers inside this directory and a routes folder inside the app layer.
28
88
Thus, your file structure should look like this:
29
89
30
90
```sh
31
91
├── src
32
92
│ ├── app
33
93
│ │ ├── routes
34
-
│ ├── pages # The pages folder assigned to FSD
94
+
│ ├── pages # Pages folder, related to FSD
35
95
```
36
96
37
-
In order for NuxtJS to use the `app` layer for file routing, you need to modify `nuxt.config.ts` as follows:
97
+
In order for NuxtJS to use the routes folder inside the `app` layer for file routing, you need to modify `nuxt.config.ts` as follows:
38
98
```ts title="nuxt.config.ts"
39
99
exportdefaultdefineNuxtConfig({
40
-
devtools: { enabled: true }, // Not FSD related, enabled at project startup
100
+
devtools: { enabled: true }, // Not FSD related, enabled at project startup
В NuxtJS есть два способа настройки роутинга - с помощью конфига и с помощью файловой структуры.
26
+
В случае с файловым роутингом вы будете создавать index.vue файлы в папках внутри директории app/routes, а в случае конфига - настраивать роуты в `router.options.ts` файле.
27
+
28
+
29
+
### Роутинг с помощью конфига
30
+
31
+
В слое `app` создайте файл `router.options.ts`, и экспортируйте из него обьект конфига:
32
+
```ts title="app/router.options.ts"
33
+
importtype { RouterConfig } from'@nuxt/schema';
34
+
35
+
exportdefault <RouterConfig> {
36
+
routes: (_routes) => [],
37
+
};
38
+
39
+
```
40
+
41
+
Чтобы добавить страницу `Home` в проект, вам нужно сделать следующие шаги:
42
+
- Добавить слайс страницы внутри слоя `pages`
43
+
- Добавить соответствующий роут в конфиг `app/router.config.ts`
44
+
45
+
46
+
Для того чтобы создать слайс страницы, воспользуемся [CLI](https://github.com/feature-sliced/cli):
47
+
48
+
```shell
49
+
fsd pages home
50
+
```
51
+
52
+
Создайте файл `home-page.vue` внутри сегмента ui, откройте к нему доступ с помощью Public API
0 commit comments