Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

navigateTo doesn't work in usefetch #27509

Closed
2536629322 opened this issue Jun 10, 2024 · 1 comment
Closed

navigateTo doesn't work in usefetch #27509

2536629322 opened this issue Jun 10, 2024 · 1 comment

Comments

@2536629322
Copy link

Environment


  • Operating System: Windows_NT
  • Node Version: v18.16.1
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: npm@9.5.1
  • Builder: -
  • User Config: devtools, ssr, nitro, app, modules, vite, css, elementPlus, plugins
  • Runtime Modules: @element-plus/nuxt@1.0.9, @pinia/nuxt@0.4.11
  • Build Modules: -

Reproduction

export const BASE_URL = process.env.BASE_URL;
import { getToken, delToken } from "#imports";

const request = () => {

const http = async (url, method, data = null, params = {}, options = {}) => {
    const token = getToken();
    const app = useNuxtApp()
    const loginInvalid = () => {
        app.runWithContext(() => {
            navigateTo('/login', { replace: true })
        })
    }
    const preOptions = {
        baseURL: BASE_URL,
        method: method,
        headers: {
            Authorization: 'Bearer ' + token,
            'Content-Type': method !== 'get' ? 'application/json' : undefined,
        },
        body: method !== 'get' ? JSON.stringify(data) : undefined,
        params: params,
        ...options,
    };


    const { data: resData, error, response } = await useFetch(url, {
        ...preOptions,
        onRequest({ request, options }) {
            // 请求前的处理
        },
        onRequestError({ request, options, error }) {
            // 处理请求错误
        },
        onResponse({ request, response, options }) {
            // 处理响应数据
            if (response.status === 401) {
                loginInvalid()
            }
        },
        onResponseError({ request, response, options }) {
            // 处理响应错误
        },
    });


    return resData.value;



};

const get = (url, params = {}, options = {}) => {
    return http(url, 'get', null, params, options);
};

const post = (url, data, params = {}, options = {}) => {
    return http(url, 'post', data, params, options);
};

const put = (url, data, params = {}, options = {}) => {
    return http(url, 'put', data, params, options);
};

const del = (url, data, params = {}, options = {}) => {
    return http(url, 'delete', data, params, options);
};

return {
    get,
    post,
    put,
    delete: del
};

};

export default request();

Describe the bug

When usefetch carries the token request interface from cookies, if the returned status code is 401, I called loginInvalid(), but navigateTo did not take effect

Additional context

No response

Logs

No response

@danielroe
Copy link
Member

useAsyncData or useFetch functions should not have side effects. They are not guaranteed to run. For example, we support payload extraction and running them at generate time.

You should also not use useFetch in order to perform a request. Instead just use $fetch directly.

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants