export function sanitizeHtml(unsafeHtml, whiteList, sanitizeFn) { if (unsafeHtml.length === 0) { return unsafeHtml } 改成jquery
时间: 2024-02-27 16:57:16 浏览: 17
在 jQuery 中,可以使用 `$` 符号来引用 jQuery 函数。下面是将该函数转换为 jQuery 的示例代码:
```javascript
$.sanitizeHtml = function(unsafeHtml, whiteList, sanitizeFn) {
if (unsafeHtml.length === 0) {
return unsafeHtml;
}
// TODO: 实现 HTML 过滤逻辑
return sanitizedHtml;
}
```
在此示例中,我们将 `sanitizeHtml` 函数定义为 `$.sanitizeHtml`,这样就可以使用 `$` 符号来访问该函数了。函数的实现逻辑与上面的 JavaScript 版本类似,但是需要注意的是,jQuery 中的函数通常是以链式调用的方式进行操作,因此在实现 HTML 过滤逻辑时需要特别注意。
相关问题
import router from './router' import store from './store' import { Message } from 'element-ui' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import { getToken } from '@/utils/auth' import { getInfo } from '@/api/login' NProgress.configure({ showSpinner: false }) const whiteList = ['/login', '/auth-redirect', '/bind', '/register','/homepage'] router.beforeEach((to, from, next) => { NProgress.start() if (getToken()) { to.meta.title && store.dispatch('settings/setTitle', to.meta.title) console.log("store.getters.roles====",store.getters.roles); console.log("getToken",getToken()) /* has token*/ if (to.path === '/login') { console.log("666666666") //判断身份非管理员跳转用户首页 if (store.getters.roles.length !== 0 && store.getters.roles.indexOf("admin") != -1 ) { console.log("7777777777777") next({ path: '/homepage' }) } else { console.log("88888888888888") next({ path: '/' }) } NProgress.done() } else { //其他路由 //角色未加载 if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息 store.dispatch('GetInfo').then(() => { store.dispatch('GenerateRoutes').then(accessRoutes => { // 根据roles权限生成可访问的路由表 router.addRoutes(accessRoutes) // 动态添加可访问路由表 next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 }) }).catch(err => { store.dispatch('LogOut').then(() => { Message.error(err) next({ path: '/' }) }) }) } else { next() } } } else { // 没有token if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next() } else { next(`/homepage?redirect=${to.fullPath}`) // 否则全部重定向到登录页 NProgress.done() } } }) router.afterEach(() => { NProgress.done() }) 这段代码我想让 store.getters.roles.length 一定存在 怎么办
你可以在 Vuex 的 state 中设置一个默认的 roles 数组,在用户登录时就将后台返回的用户角色存储到这个数组中。这样在 beforeach 中判断 store.getters.roles.length 是否为 0 时,就不会出现不存在的情况了。同时也要注意在用户退出登录时清空这个 roles 数组。
axios拦截器判断toke是否过期怎么做function (request) { const token = window.localStorage.getItem("poying_access_token"); request.headers.Authorization = request?.headers?.Authorization ?? (Bearer ${token} || ""); if (!whiteList.some((d) => d === request.url) && !token) { window.location.href = "/login"; } return request; }, function (error) { return Promise.reject(error); } );
这段代码中,使用了axios的请求拦截器,在请求发出前进行token的判断。具体的做法是从本地存储中获取token,然后将token添加到请求头的Authorization字段中。如果请求的url不在白名单中并且没有token,则跳转到登录页面。最后,将请求对象返回,使请求继续发送出去。
如果token过期了,可以通过后端返回的状态码或错误信息来判断,然后在响应拦截器中进行处理。例如,如果后端返回401状态码或者错误信息中包含token过期的字样,那么可以在响应拦截器中进行token刷新或者跳转到登录页面。具体的做法可以参考下面的代码:
```
axios.interceptors.response.use(
function (response) {
return response;
},
function (error) {
if (error.response.status === 401 || error.response.data.includes('token expired')) {
// 进行token刷新或跳转到登录页面
// ...
}
return Promise.reject(error);
}
);
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)