-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
226 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# Hello VitePress | ||
# vue3 oop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Component } from '@/di' | ||
import { VueComponent } from '@/index' | ||
import { UserService } from './module/auth/user.service' | ||
import { Button, Col, ConfigProvider, Row } from 'ant-design-vue' | ||
import zhCN from 'ant-design-vue/es/locale/zh_CN' | ||
import { RouterView } from 'vue-router' | ||
import { RouterService } from './router/router.service' | ||
import { http, HTTP_CLIENT } from './api/http' | ||
import { CountService } from './count.service' | ||
|
||
@Component({ | ||
providers: [UserService, RouterService, { provide: HTTP_CLIENT, useValue: http }, CountService], | ||
}) | ||
export class App extends VueComponent { | ||
constructor( | ||
private userService: UserService, | ||
private routerService: RouterService, | ||
private countService: CountService, | ||
) { | ||
super() | ||
} | ||
render() { | ||
return ( | ||
<ConfigProvider locale={zhCN}> | ||
<h2 style={{ textAlign: 'center' }}>全局服务</h2> | ||
<Row justify={'center'}> | ||
<Col> | ||
<Button type={'primary'} onClick={this.countService.add}> | ||
加 | ||
</Button> | ||
<span style={{ fontSize: '24px', margin: '0 20px' }}>{this.countService.count}</span> | ||
<Button type={'primary'} danger onClick={this.countService.remove}> | ||
减 | ||
</Button> | ||
</Col> | ||
</Row> | ||
<RouterView></RouterView> | ||
</ConfigProvider> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Autobind, Ref, VueService } from '@/index' | ||
|
||
export class CountService extends VueService { | ||
@Ref() count = 0 | ||
|
||
@Autobind() | ||
add() { | ||
this.count++ | ||
} | ||
|
||
@Autobind() | ||
remove() { | ||
this.count-- | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,8 @@ | ||
import '@abraham/reflection' | ||
import { createApp } from 'vue' | ||
import { VueComponent } from '@/extends/component' | ||
import { RouterView } from 'vue-router' | ||
import { setupRouter } from './router' | ||
import { Component } from '@/di' | ||
import { UserService } from './module/auth/user.service' | ||
import './theme/app.css' | ||
import 'ant-design-vue/dist/antd.css' | ||
import { Button, ConfigProvider, Table } from 'ant-design-vue' | ||
import zhCN from 'ant-design-vue/es/locale/zh_CN' | ||
import { setupHttp } from './api/http' | ||
|
||
@Component({ autoResolveDeps: true, globalStore: true }) | ||
class App extends VueComponent { | ||
constructor(private userService: UserService) { | ||
super() | ||
} | ||
render() { | ||
return ( | ||
<ConfigProvider locale={zhCN}> | ||
<h2 style={{ textAlign: 'center' }}>全局服务</h2> | ||
<RouterView></RouterView> | ||
</ConfigProvider> | ||
) | ||
} | ||
} | ||
import { App } from './app' | ||
|
||
const app = createApp(App) | ||
setupRouter(app) | ||
setupHttp(app) | ||
app.mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,40 @@ | ||
import { VueService } from '@/extends/service' | ||
import { Injectable } from 'injection-js' | ||
import { Hook, Ref, VueService } from '@/index' | ||
import { Inject, Injectable } from 'injection-js' | ||
import { RouterService } from '../../router/router.service' | ||
import { Ref } from '@/index' | ||
import { AxiosInstance } from 'axios' | ||
import { HTTP_CLIENT } from '../../api/http' | ||
|
||
@Injectable() | ||
export class UserService extends VueService { | ||
constructor(private routerService: RouterService) { | ||
constructor(private routerService: RouterService, @Inject(HTTP_CLIENT) private httpService: AxiosInstance) { | ||
super() | ||
this.guardHttp() | ||
this.guardRouter() | ||
} | ||
@Ref() token?: string | ||
@Ref() token = '' | ||
|
||
private _requestGuard: number | ||
|
||
guardHttp() { | ||
this._requestGuard = this.httpService.interceptors.request.use((config) => { | ||
return config | ||
}) | ||
} | ||
|
||
guardRouter() { | ||
this.routerService.router.beforeEach(async (to, from) => { | ||
if (to.path === '/login' && this.token) return { path: '/' } | ||
if (to.path !== '/login' && !this.token) return { path: '/login' } | ||
}) | ||
} | ||
|
||
@Hook('BeforeUnmount') | ||
unmount() { | ||
this.httpService.interceptors.request.eject(this._requestGuard) | ||
} | ||
|
||
async login(model: any) { | ||
this.token = await new Promise((resolve) => setTimeout(resolve, 3000, 'token')) | ||
this.routerService.router.replace('/') | ||
this.token = await new Promise((resolve) => setTimeout(resolve, 1000, 'token')) | ||
await this.routerService.router.replace('/') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,44 @@ | ||
import { VueComponent } from '@/extends/component' | ||
import { Component, VueComponent } from '@/index' | ||
import { CountService } from '../../count.service' | ||
import { Optional, SkipSelf } from 'injection-js' | ||
import { Button, Col, Row } from 'ant-design-vue' | ||
import { watch } from 'vue' | ||
|
||
@Component({ | ||
providers: [CountService], | ||
}) | ||
export default class HomeView extends VueComponent { | ||
constructor( | ||
@SkipSelf() private parentCountService: CountService, | ||
private countService: CountService, | ||
@Optional() private aaa: string, | ||
) { | ||
super() | ||
watch( | ||
() => this.parentCountService.count, | ||
() => (countService.count = parentCountService.count), | ||
{ | ||
immediate: true, | ||
}, | ||
) | ||
} | ||
render() { | ||
return <div>home</div> | ||
return ( | ||
<> | ||
<h3 style={{ textAlign: 'center' }}>全局的状态: {this.parentCountService.count}</h3> | ||
<h3 style={{ textAlign: 'center' }}>局部的状态</h3> | ||
<Row justify={'center'}> | ||
<Col> | ||
<Button type={'primary'} onClick={this.countService.add}> | ||
加 | ||
</Button> | ||
<span style={{ fontSize: '24px', margin: '0 20px' }}>{this.countService.count}</span> | ||
<Button type={'primary'} danger onClick={this.countService.remove}> | ||
减 | ||
</Button> | ||
</Col> | ||
</Row> | ||
</> | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
import { Injectable } from 'injection-js' | ||
import { useRouter } from 'vue-router' | ||
import { createRouter, createWebHistory } from 'vue-router' | ||
import { routes } from './routes' | ||
import { getCurrentApp, VueService } from '@/index' | ||
|
||
@Injectable() | ||
export class RouterService { | ||
router = useRouter() | ||
export class RouterService extends VueService { | ||
history = createWebHistory() | ||
router = createRouter({ | ||
history: this.history, | ||
routes: routes, | ||
}) | ||
app = getCurrentApp()! | ||
get currentRoute() { | ||
return this.router.currentRoute.value | ||
} | ||
|
||
constructor() { | ||
super() | ||
this.app.use(this.router) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.