ssr: why i don't found the value of {{ test }} in the source page #15807
-
there is something i don't understand, in my template app.vue i put an interpolation like this {{ this.test }}
<script>
export default {
name: 'App',
components: {
},
data() {
return {
test : 'hello'
)
}
}
</script>
i do : quasar dev - m ssr and the problem is that hello is not in the source page (ctrl+u on chrome) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
You can only find it when first visiting the page. After hydration is completed, it will change to a single-page application (SPA). |
Beta Was this translation helpful? Give feedback.
-
Do not use
|
Beta Was this translation helpful? Give feedback.
-
so i had to write {{ test }} but also clear the page now i see the hello . ok now the problem is that i spend yesterday the entire journey to understand how to make my axios call executed before rendered that is called with the help of ssrContext.rendered = () => { i prefer the hook created that the hook prefetch because my component methods are not available in prefetch and i have to redo all the code of my method in prefetch. how to wait for the created method to finish . i tried with a promise like this
but the problem is that "rendered" is printed before the "translation :" edit: ok if there is a prefech method it's that it's the way to do it . let me try a promise in prefetch ... so to pass the data retrieve from axios i have to use a store ... let me try with pinia ... |
Beta Was this translation helpful? Give feedback.
-
a note, u can expose the axios globaly, as long u don't modify your defaults with user info: import axios from 'axios'
import { boot } from 'quasar/wrappers'
const api = axios.create({ /*...*/ })
export function updateToken() {
api.defaults.headers.common.Authorization = getTokenSomehow();
}
export default boot(function ({ app, store }) {
updateToken()
app.config.globalProperties.$api = api
})
export { api } this is also bad.: import axios from 'axios'
import { boot } from 'quasar/wrappers'
const api = axios.create({ /*...*/ })
export default boot(function ({ app, store }) {
api.interceptors.request.use(function (config) {
const token = getTokenSomehow();
if (token) {
config.headers ||= {};
config.headers.Authorization = token;
}
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
app.config.globalProperties.$api = api
})
export { api } said that, what i recommend is: import axios from 'axios'
import { boot } from 'quasar/wrappers'
export const apiKey = 'api-key'
export default boot(function ({ app, store }) {
const api = axios.create({ /*...*/ })
app.provide(apiKey, api)
// this line is very important, without that u can't acess the api in the actions
store.use(() => ({ api }))
/*
api.interceptors.request.use(function (config) {
const token = getTokenSomehow();
if (token) {
config.headers ||= {};
config.headers.Authorization = token;
}
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
*/
}) so, u'll be able to access your api in the components like that: import { defineComponent } from 'vue'
import { apiKey } from 'src/boot/api'
export default defineComponent({
inject: {
api: apiKey
},
methods: {
async fetch () {
const { data } = await this.api.get('...')
}
}
}) composition api <script setup>
import { inject } from 'vue'
import { apiKey } from 'src/boot/api'
const api = inject(apiKey)
async function fetch () {
const { data } = await this.api.get('...')
}
</script> now, let's talk about the preFetch itself, since u'll not be able to inject anything here, u would do all your async stuff in the action.: import { defineStore } from 'pinia'
export const useMyStore = defineStore('my', {
state: () => ({
list: []
}),
actions: {
async fetch () {
const { data } = await this.api.get('...')
this.list = data;
}
}
}) setup store import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useMyStore = defineStore('my', () => {
const list = ref([])
async function fetch () {
const { data } = await this.api.get('...')
list.value = data;
}
return {
list,
data
}
}) finally, the preFetch example: import { defineComponent } from 'vue'
import { preFetch } from 'quasar/wrappers'
import { mapStores } from 'pinia'
import { useMyStore } from 'src/stores/my'
export default defineComponent({
preFetch: preFetch(async ({ store }) => {
const myStore = useMyStore(store)
await myStore.fetch()
}),
mounted() {
console.log(this.myStore.list)
},
computed: {
...mapStores(useMyStore)
}
}) composition api <script>
import { defineComponent } from 'vue'
import { preFetch } from 'quasar/wrappers'
import { useMyStore } from 'src/stores/my'
export default defineComponent({
preFetch: preFetch(async ({ store }) => {
const myStore = useMyStore(store)
await myStore.fetch()
})
})
</script>
<script setup>
import { onMounted } from 'vue'
import { useMyStore } from 'src/stores/my'
import { storeToRefs } from 'pinia'
const myStore = useMyStore()
const { list } = storeToRefs(myStore)
onMounted(() => {
console.log(list.value)
})
</script> |
Beta Was this translation helpful? Give feedback.
Do not use
this.*
in your SFC template (you used{{ this.test }}
). Directly mention the variables in your component scope.