diff --git a/packages/fes-plugin-request/package.json b/packages/fes-plugin-request/package.json index d6ca6b4f..2326fedf 100644 --- a/packages/fes-plugin-request/package.json +++ b/packages/fes-plugin-request/package.json @@ -27,6 +27,7 @@ "access": "public" }, "peerDependencies": { + "vue": "^3.0.4", "@webank/fes": "^2.0.0-alpha.0" }, "dependencies": { diff --git a/packages/fes-plugin-request/src/template/request.js b/packages/fes-plugin-request/src/template/request.js index aa894287..fad41688 100644 --- a/packages/fes-plugin-request/src/template/request.js +++ b/packages/fes-plugin-request/src/template/request.js @@ -1,5 +1,6 @@ import axios from 'axios'; import { ApplyPluginsType, plugin } from '@webank/fes'; +import { ref } from 'vue'; import scheduler from './scheduler'; import { checkHttpRequestHasBody, @@ -126,3 +127,31 @@ export const request = (url, data, options = {}) => { return Promise.reject(context.error); }); }; + +function isPromiseLike(obj) { + return !!obj && typeof obj === 'object' && typeof obj.then === 'function'; +} + +export const useRequest = (url, data, options = {}) => { + const loadingRef = ref(true); + const errorRef = ref(null); + const dataRef = ref(null); + let promise; + if (isPromiseLike(url)) { + promise = url; + } else { + promise = request(url, data, options); + } + promise.then((res) => { + dataRef.value = res; + }).catch((error) => { + errorRef.value = error; + }).finally(() => { + loadingRef.value = false; + }); + return { + loading: loadingRef, + error: errorRef, + data: dataRef + }; +}; diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/index.js b/packages/fes-preset-built-in/src/plugins/misc/route/index.js index d978fe30..13869371 100644 --- a/packages/fes-preset-built-in/src/plugins/misc/route/index.js +++ b/packages/fes-preset-built-in/src/plugins/misc/route/index.js @@ -170,7 +170,7 @@ const fix = function (routes) { const getRoutes = function ({ config, absPagesPath }) { // 用户配置了routes则使用用户配置的 - if (config.routes) return config.routes; + if (config.router.routes) return config.router.routes; const routes = []; genRoutes(routes, absPagesPath, '/', config); @@ -215,7 +215,17 @@ const getRoutesJSON = function ({ routes, config }) { export default function (api) { api.describe({ - key: 'routes' + key: 'router', + config: { + schema(joi) { + return joi + .object({ + routes: joi.array(), + mode: joi.string() + }); + }, + default: {} + } }); api.registerMethod({ @@ -259,7 +269,8 @@ export default function (api) { runtimePath, routes, config: api.config, - routerBase: api.config.base || '' + routerBase: api.config.base || '', + CREATE_HISTORY: api.config.router.mode === 'history' ? 'createWebHistory' : 'createWebHashHistory' }) }); diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/template/routes.tpl b/packages/fes-preset-built-in/src/plugins/misc/route/template/routes.tpl index 9fb5665b..651ff137 100644 --- a/packages/fes-preset-built-in/src/plugins/misc/route/template/routes.tpl +++ b/packages/fes-preset-built-in/src/plugins/misc/route/template/routes.tpl @@ -1,4 +1,4 @@ -import { createRouter as createVueRouter, createWebHashHistory, ApplyPluginsType } from '{{{ runtimePath }}}'; +import { createRouter as createVueRouter, {{{ CREATE_HISTORY }}}, ApplyPluginsType } from '{{{ runtimePath }}}'; import { plugin } from '@@/core/coreExports'; export function getRoutes() { @@ -19,8 +19,9 @@ export const createRouter = () => { if (router) { return router; } + const history = {{{ CREATE_HISTORY }}}(ROUTER_BASE) router = createVueRouter({ - history: createWebHashHistory(ROUTER_BASE), + history, routes: getRoutes() }); diff --git a/packages/fes-runtime/src/index.js b/packages/fes-runtime/src/index.js index ba05bd49..c1a2691b 100644 --- a/packages/fes-runtime/src/index.js +++ b/packages/fes-runtime/src/index.js @@ -7,6 +7,7 @@ export { RouterLink, useLink, createWebHashHistory, + createWebHistory, createRouter } from 'vue-router'; diff --git a/packages/fes-template-h5/src/pages/index.vue b/packages/fes-template-h5/src/pages/index.vue index 49832b68..4168d0b4 100644 --- a/packages/fes-template-h5/src/pages/index.vue +++ b/packages/fes-template-h5/src/pages/index.vue @@ -2,6 +2,8 @@
fes & 拉夫德鲁
+
loading
+
{{data}}
@@ -12,7 +14,7 @@