PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label vue-script-setup. Show all posts
Showing posts with label vue-script-setup. Show all posts

Sunday, October 16, 2022

[FIXED] How does Computed work in Vue 3 script setup?

 October 16, 2022     vue-composition-api, vue-script-setup, vue.js, vuejs3     No comments   

Issue

I'm trying to get computed to work in <script setup>:

<template>
  <div>
    <p>{{ whatever.get() }}</p>
  </div>
</template>


<script setup>
import {computed} from "vue";

const whatever = computed({
    get() {
        console.log('check')
        return 'check?';
    }
})
</script>

The console.log() comes through but the return statement seems to throw an error:

check
Vue warn]: Unhandled error during execution of render function 
  at <Cms>
Uncaught TypeError: $setup.whatever.get is not a function
at Proxy.render (app.js?id=d9e007128724c77a8d69ec76c6c081a0:39550:266)
at renderComponentRoot (app.js?id=d9e007128724c77a8d69ec76c6c081a0:25902:44)
at ReactiveEffect.componentUpdateFn [as fn] (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30019:57)
at ReactiveEffect.run (app.js?id=d9e007128724c77a8d69ec76c6c081a0:23830:29)
at setupRenderEffect (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30145:9)
at mountComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29928:9)
at processComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29886:17)
at patch (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29487:21)
at render (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30630:13)
at mount (app.js?id=d9e007128724c77a8d69ec76c6c081a0:28882:25)

What am I doing wrong?


Solution

In <template>:

Getter only:

  • {{ myVal }}
  • <p v-text="myVal" />

Getter and setter:

  • <input v-model="myVal">

Important: Do not use myVal.get() or myVal.set(value)! Use:

  • getting: myVal
  • setting (assignment): myVal = value

In <script setup>:

Getter only:

const someReactiveRef = ref(null)
const myVal = computed(() => someReactiveRef.value)

Getter & setter:

const someReactiveRef = ref(null)

const myVal = computed({
  get() {
    return someReactiveRef.value
  },
  set(val) {
    someReactiveRef.value = val
  }
})
// myVal can now be used in `v-model`

When the reactive ref is coming from a reactive() object's property, you don't need the .value in either the setter or the getter. Example:

const state = reactive({
  someProp: null
})

const myVal = computed({
  get() {
    return store.someProp
  },
  set(val) {
    store.someProp = val
  }
})

Remember you can always use computed inside reactive. Example:

const { createApp, reactive, computed, toRefs } = Vue
createApp({
  setup() {
    const state = reactive({
      firstName: 'John W.',
      lastName: 'Doe',

      // getter + setter
      fullName: computed({
        get() {
          return [state.firstName, state.lastName].join(' ')
        },
        set(val) {
          const [last, ...rest] = val.split(' ').reverse()
          state.firstName = rest.reverse().join(' ')
          state.lastName = last
        }
      }),

      // getter only:
      name: computed(() => state.fullName)
    })
    return toRefs(state)
  }
}).mount('#app')
<script src="https://unpkg.com/vue@3.2.40/dist/vue.global.prod.js"></script>
<div id="app">
  <input v-model="firstName" />
  <input v-model="lastName" />
  <input v-model="fullName" />
  <pre v-text="{ firstName, lastName, fullName, name }"></pre>
</div>

Important notes:

  • when passing a non-reactive reference to a computed, Vue will warn you (the computed wrapper is unnecessary).
  • when passing it a ref which contains a cyclic dependency (e.g: a component instance) Vue will again warn you and advise on using either shallowRef or markRaw.


Answered By - tao
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, October 15, 2022

[FIXED] How to scrollIntoView in vue composition api that use script setup?

 October 15, 2022     dom, javascript, vue-composition-api, vue-script-setup, vuejs3     No comments   

Issue

I have a parent app(parent.vue) that emit value from child app(MainMenu.vue) that used to scroll in spesific pageview by id in parent app, but when i run it give the error message TypeError: document.getElementById(...) is null.

This is the parent app that i want to scroll in spesific pageview

parent.vue

<template>
  <section>
    <MainCover></MainCover>
    <QuotePage></QuotePage>
    <GroomBride id='1'></GroomBride>
    <TimeLines id='2'></TimeLines>
    <RunDown id='3'></RunDown>
    <SpecialInvitation></SpecialInvitation>
    <LiveStream></LiveStream>
    <OurMoment id='4'></OurMoment>
    <MessageBox id='5'></MessageBox>
    <FooterPage></FooterPage>
    <MainMenu @coba="onCoba" @page="navigation"></MainMenu>
  </section>
</template>

<script setup>
import QuotePage from '@/components/QuotePage.vue'
import MainCover from '@/components/MainCover.vue'
import GroomBride from '@/components/GroomBride.vue'
import TimeLines from '@/components/TimeLines.vue'
import RunDown from '@/components/RunDown.vue'
import SpecialInvitation from '@/components/SpecialInvitation.vue'
import OurMoment from '@/components/OurMoment.vue'
import MessageBox from '@/components/MessageBox.vue'
import FooterPage from '@/components/FooterPage.vue'
import MainMenu from '@/components/MainMenu.vue'
import LiveStream from '@/components/LiveStream.vue'


// Navigation handler
const navigation = val => {
  console.log(val);
  document.getElementById(val).scrollIntoView({
    behavior: 'smooth'
  })
}

</script>

This is the child app that i emit a value to parent,

MainMenu.vue

<template>
  <div class="container">
      <div class="notification is-primary p-2">
              <!-- <span class="mr-4" @click="editEvent(day.id, event.details)"><fa :icon="['fas', 'pencil']" /></span>
              <span @click="deleteEvent(day.id, event.details)"><fa :icon="['fas', 'trash']" /></span> -->
          <span class="icon-text is-paddingless is-marginless">
              <span @click="navPage('1')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-heart" />
              </span>
              <!-- <span>Couple</span> -->
              <span @click="navPage('2')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-calendar-days" />
              </span>
              <!-- <span>Date</span> -->
              <span @click="navPage('3')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-map" />
              </span>
              <!-- <span>Location</span> -->
              <span @click="navPage('4')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-images" />
              </span>
              <!-- <span>Galleries</span> -->
              <span @click="navPage('5')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-pen-to-square" />
              </span>
              <!-- <span>Wishes</span> -->
          </span>
      </div>
  </div>
</template>

<script setup>
import { defineEmits } from "vue";

const emit = defineEmits(['page']);


const navPage = (value) => {
    emit("page", value)
}

</script>

I consider that the error is in the accessing dom from script setup. I have tried but did'nt get solution, please help. Thanks


Solution

Its done, i update my code

MainMenu.vue

<template>
  <div class="container">
      <div class="notification is-primary p-2">
          <span class="icon-text is-paddingless is-marginless">
              <span @click="navPage('groom')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-heart" />
              </span>
              <!-- <span>Couple</span> -->
              <span @click="navPage('time')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-calendar-days" />
              </span>
              <!-- <span>Date</span> -->
              <span @click="navPage('run')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-map" />
              </span>
              <!-- <span>Location</span> -->
              <span @click="navPage('moment')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-images" />
              </span>
              <!-- <span>Galleries</span> -->
              <span @click="navPage('message')" class="icon">
                  <font-awesome-icon icon="fa-solid fa-pen-to-square" />
              </span>
              <!-- <span>Wishes</span> -->
          </span>
      </div>
  </div>
</template>

<script setup>
import { defineEmits } from "vue";

const emit = defineEmits(['page']);


const navPage = (value) => {
    emit("page", value)
}

</script>

parent.vue

<template>
  <section>
    <MainCover></MainCover>
    <QuotePage></QuotePage>
    <div ref="groom">
      <GroomBride></GroomBride>
    </div>
    <div ref="time">
      <TimeLines></TimeLines>
    </div>
    <div ref='run'>
      <RunDown></RunDown>
    </div>
    <SpecialInvitation></SpecialInvitation>
    <LiveStream></LiveStream>
    <div ref='moment'>
      <OurMoment></OurMoment>
    </div>
    <div ref='message'>
      <MessageBox></MessageBox>
    </div>
    <FooterPage></FooterPage>
    <MainMenu @page="navigation"></MainMenu>
  </section>
</template>

<script setup>
import QuotePage from '@/components/QuotePage.vue'
import MainCover from '@/components/MainCover.vue'
import GroomBride from '@/components/GroomBride.vue'
import TimeLines from '@/components/TimeLines.vue'
import RunDown from '@/components/RunDown.vue'
import SpecialInvitation from '@/components/SpecialInvitation.vue'
import OurMoment from '@/components/OurMoment.vue'
import MessageBox from '@/components/MessageBox.vue'
import FooterPage from '@/components/FooterPage.vue'
import MainMenu from '@/components/MainMenu.vue'
import LiveStream from '@/components/LiveStream.vue'

import { ref, onMounted } from "vue";

const groom = ref()
const time = ref()
const run = ref()
const moment = ref()
const message = ref()


// // Navigation handler
const navigation = val => {
  console.log(val)
  if (val == 'groom') {
    groom.value.scrollIntoView({behavior: "smooth"})
  } else if (val == 'time') {
    time.value.scrollIntoView({behavior: "smooth"})
  } else if (val == 'run') {
    run.value.scrollIntoView({behavior: "smooth"})
  } else if (val == 'moment') {
    moment.value.scrollIntoView({behavior: "smooth"})
  } else if (val == 'message') {
    message.value.scrollIntoView({behavior: "smooth"})
  }
}


onMounted( () => {
  navigation();
})

</script>


Answered By - cahya faisal reza
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing