The tale of the Vue Composition API and the mysterious router that vanished into thin air

When attempting to display a component using storybook (vue-cli conf) with the vue-composition-api, an error is encountered:

Error: Cannot read property '_router' of undefined

The code from file.stories.ts is as follows:

import Vue from 'vue'
import CompositionApi from '@vue/composition-api'
import VueRouter from 'vue-router'

import { storiesOf } from '@storybook/vue'
import Login from '@/ui/views/Login/Login.vue'
import StoryRouter from 'storybook-vue-router'

Vue.use(CompositionApi)
Vue.use(VueRouter)

storiesOf('Components', module)
  .addDecorator(StoryRouter())
  .add('Login', () => ({
    components: { Login },
    template: '<Login/>',
  }))

The code from file.vue is provided below

<template>
... 
</template>

export default defineComponent({
  name: 'Login',
  setup(props, { root: { $router } }) {

    function redirectTo() {
       $router.push({ name: 'Home' })
    }

    return {
       redirectTo,
    }

  },
})

Changing setup(props, { root }) to setup(props, { root.$router(...) resolves the issue. However, there is a desire to maintain the destructuring in the setup function. Any suggestions on how this can be achieved?

Answer №1

When it comes to storybook, router, and vuetify plugins setup in a Vue app, they all belong to the parent of the component. This means that you need to bind them to the component itself.

If you want to extend a component, you can do so like this:

const extendedComponent = Vue.component(
  componentName,
  {
    extends: component.default || component,
    router,
    vuetify
  }
)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Path to local image in JavaScript

Apologies for the newbie question, but I'm trying to display a gif on a webpage using an HTTP link and it works perfectly. However, when I try to replace it with a file path, it doesn't work. The gif is located in the same folder as the webpage. ...

Constructing a hierarchical tree in angularJS

I need to create a dynamic tree structure in AngularJS with data retrieved from the database. The structure consists of different levels as shown below: [ { "TreeName": "String content", "Level": 0 }, { "TreeName": "String content", ...

Discovering techniques to access nested objects in JavaScript

My initial object state looks like this: answersCount: { Colors: { Green: 0, Brown: 0, Blue: 0, Red: 0 }, Letters: { A: 0, B: 0, C: 0, D: 0, }, Briggs: { EI: 0, SN: 0, T ...

Combine all the HTML, JavaScript, and CSS files into a single index.html file

So here's the situation - I am utilizing a C# console application to convert four XML files into an HTML document. This particular document will be circulated among a specific group of individuals and is not intended to be hosted or used as a web app; ...

Vue application experiences issues with styles not displaying as expected

I've recently put together my very first Vue web application. As I was building it, I ran into an issue with my delete buttons not displaying correctly. No matter what styling adjustments I made, the problem persisted. Any suggestions on how to fix th ...

Include variables in a JavaScript statement to create conditional functionality

Currently, my code is functioning as expected and appears like this: if (Number.isInteger(number)) { ffmpeg(video.mp4) .on('end', function () { console.log('Screenshots taken'); }) .screenshots ...

Iterate through a JavaScript grid and display the items

I am attempting to iterate through a JavaScript table, aiming to display the elements located between two selections made by the user. Here is an example of the table structure: if the user selects IDs 2 and 5, the loop should display rows 3 and 4. I trie ...

Determine the screen positions of the top edge of the bounding box in a three.js scene

On my three.js page, I have a set of particles rendering (you can find the demo and code here: ). This is how the default view looks like, with a unit cube centered at (0, 0, 0): To reveal the transparent bounding boxes surrounding each particle inside t ...

How to properly import components and CSS in Vue.js for optimized performance

As a beginner in vue.js, I find one particular concept to be quite confusing. Can someone help clarify the difference between importing a vue.js "component" and a CSS file? For example, when considering bootstrap and vue-bootstrap, do I need both? Just o ...

Finding an element with Protractor by executing JS script

My objective is to retrieve a drop-down list of elements. Despite trying Protractor's methods, I struggled to easily locate isolate-span elements. Due to this, I am now turning to JavaScript code: var my_js_element = browser.executeScript(jQuery("td. ...

Updating the src attribute of an image using document.getElementByClassName along with the ngif directive

I am attempting to dynamically modify different icon images on page load using document.getElementsByClassName. The icons are used multiple times and their visibility is controlled by the ngIf directive based on user actions. <img class="viewlist_b ...

Looking to enhance code by using jQuery to substitute numerous href elements. Only seeking enhancements in code quality

I am currently using regular JavaScript to change the href of 3 a-tags, but I want to switch to jQuery for this task. var catNav = $('ul.nav'), newLink = ['new1/','new2','nwe3/']; catNav.attr('id','n ...

typescript: Imported modules in typescript are not functioning

I'm facing an issue where I installed the 'web-request' module but unable to get it working properly. Here is my code: npm install web-request After installation, I imported and used it in my class: import * as WebRequest from 'web-r ...

What methods can I implement to ensure a button illuminates only when correct information is provided?

I have developed a calculator for permutations and combinations. When either permutation or combination is selected, and the required values are entered, I want the calculate button to light up. How can I achieve this without using setInterval in my curren ...

Angular Component not reflecting changes to property when array is reversed

Within my Angular Component, I have a public array variable that is initially set to an empty array. Upon receiving a response from a service in the constructor, I attempt to update the array by using this.variable = array.reverse(). However, instead of ...

Error: Property "rotation" cannot be redefined

After setting up all the necessary dependencies using npm and bower for a project on a different laptop, I encountered an error when trying to run it locally. The error message reads: Uncaught TypeError: Cannot redefine property: rotation at (line 7542 i ...

Modal opening leading to loss of event bindings on the background page

I created a webpage that has links to modals. There is a search bar named #top-search at the top of this page. I added an event in my main JavaScript file within the ready function: $('#top-search').keypress(function (e) { if (e.which ...

Why is it that my for loop produces the accurate result, yet my forEach function fails to do so?

Hey there, I'm new to SO and seeking some guidance. I have a task where I need to create a function with two parameters - an array of strings and a string to potentially match within the array. I've developed two versions of the function: one us ...

Is it possible to stop the manipulation of HTML and CSS elements on a webpage using tools similar to Firebug?

How can we prevent unauthorized editing of HTML and CSS content on a webpage using tools like Firebug? I have noticed that some users are altering values in hidden fields and manipulating content within div or span tags to their advantage. They seem to be ...

What is the best method for converting Unix time to a readable date

<el-table-column label="Time Created" prop="create_time"></el-table-column> https://i.stack.imgur.com/aoLjf.png The timestamp data from the backend is in milliseconds format (e.g. 1527150668419) and is stored as create_time property in this.i ...