Tips for dividing a vue.js template into a .vue file while keeping the code in .js

I am trying to organize my Vue.js template by separating it into a .vue file, while keeping the code in a .js file.

Here is an example of a Vue "single file component":

<template>
..
</template>

<script>
..
</script>

Current Solution:

I have made the .vue file the main component and used a MIXIN to reference the .js file. This approach seems a bit messy!

Preferred Approach:

I would like to be able to import the .js file with just one webpack import:

...
</template>

import ViewModel from 'vm.js'

Is there a way to do this? Or perhaps use a <script> tag in the .vue file that can be understood by vue-loader?

<template>
..
<script src='vm.js'/>
</template>

Thank you for your help! My aim is to be able to set breakpoints in Visual Studio Code, maintain separation of concerns, and improve readability. :)

Additionally, I need this solution to work with TypeScript!

Answer №1

...
</template>

import Module from 'vm.js'

An alternative approach involves a slight modification to the second option

<template>
...
</template>

<script src="module.js"></script>

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

Is there a CSS3 Selector With Similar Functionality to jQuery's .click()?

For a few years now, I have been utilizing a pure CSS navigation system. However, with the recent increase in mobile site projects at my workplace, I am encountering issues with drop-down menus not functioning properly on mobile devices. Despite this chall ...

Issue with Jquery UI draggable positioning in a specific Reveal.js slide

While my jQuery draggable feature works flawlessly in a simple HTML page without the presence of reveal.js, I encounter an issue within my reveal.js presentation where I utilize embed=true [utilizing only a portion of the full page for each slide]. When i ...

Having trouble with AutoCompleteExtender OnClientItemSelected not functioning in IE8 but working perfectly in IE9? Let's dive into the JavaScript substring

My AutoCompleteExtender is connected to a web service and works perfectly. The Target TextBox (tb_provider1) has autocomplete capabilities thanks to the GetProviders function. What I want to achieve is triggering a javascript function when a user selects a ...

Trouble with $.getJSON while trying to obtain song tempo data with getSongBPM

Still learning the ropes, so bear with me. I am trying to retrieve the BPM of a song using the getSongBPM public API. However, I'm not seeing any data in the console. // Please note that the API key has been updated $.getJSON("https://api.getsongbp ...

When trying to ctrl-click on paths in Visual Studio Code, the resolution is incorrect

I'm currently working on customizing a library by modifying its source code. I've downloaded the source code and now I'm trying to configure vscode, typescript, and webpack to recognize that file. Although I've successfully aliased the ...

Refreshing the DOM following an API call using VueJS

Struggling with updating the DOM after fetching data from an API. Although my object is successfully fetching the data, the DOM renders before receiving the API Data and fails to update afterward. I'm puzzled as to why it's not refreshing itself ...

Transmitting various pieces of information using AJAX

Is it possible to send both "credit_uri" and "address" strings in one AJAX request? Currently, only the second string is being sent. How can I include both in the data of the request? $.ajax({ url: '#{add_cards_path}', type: 'POST&apo ...

Encountered a discrepancy with npm dependencies during the update or installation process on a legacy project

I am encountering issues while attempting to run an older project. Every time I try to npm install or start the project, it throws various dependency errors, particularly related to the outdated npm version. My current node version is 16.x, whereas the pro ...

Is it necessary for React to pass onClick as props to sub components?

Attempting to activate an onClick function within a sub-component by including the onClick in the parent component did not yield the desired outcome. // parent component class MainForm extends Component { // code here handleClick = () => { con ...

Guide to creating AngularJS directive attributes without a right-hand side in hiccup code?

I'm currently developing an AngularJS application using markup in hiccup format. Here is a snippet of the markup: <div modal-show modal-visible="showDialog" .........></div> Below is the corresponding Hiccup I have created: [:div.modal. ...

Storing items in localStorage in the same order they were added

I am having an issue with sorting localStorage items by the order they were added. Despite my code working to add items to the localStorage array and display them as HTML, I am encountering a problem with the order of the items. When I add a 3rd item to t ...

Tips for setting up a Vue.js property as an array type element

To begin, I am looking to initialize the properties of image, description, video, and title with the corresponding fields from the first element in the variants array. The variants array is retrieved by sending an AJAX request that returns a JSON file. ...

Nextjs: where all roads lead back to the homepage

Having an issue in my app where all redirects keep leading back to the homepage. The problem seems to stem from my Navbar.js component, as shown below. Despite adding the required route in the Link tag for next-js, both client and server compilation is suc ...

passport.js and express.js working together to seamlessly redirect users back to the original page after completing oauth authentication

How can I navigate users to the same page after an OAuth request to Twitter by setting a session variable? Here is how the routes are currently configured: // Authentication routes (function () { app.get('/auth/twitter', passport.authentica ...

Prevent Mui popover from closing when clicking outside

Currently, I have implemented Mui Popover with ReactJS from the link here. One issue I am facing is that when a user clicks outside the popover, it automatically closes. Is there a way to disable this default behavior? Additionally, I would like to add a ...

Sort through a collection of objects based on their content

I'm having some trouble filtering an array of objects based on its content. Here's the code snippet: https://jsfiddle.net/z7g3unyu/2/ var arr =[ { "uid": "1", "name": "John Doe" }, { "uid": "2", "name": "Kate Roe" } ]; ...

The canvas will not show up, but it will magically appear on its own

Having trouble incorporating this script within a section or any other element? The script works perfectly on its own, but once loaded within a section, it fails to function properly. Below is the script in question; <!-- partial:index.partial.html --& ...

Click on the button to automatically scroll to a specific component inside the dialog

In my JSF/Primefaces application, I have a page with a long content that can be scrolled, along with a dialog also containing lengthy content that requires scrolling. The dialog includes a button and I am looking to scroll the dialog content to a specific ...

When users click on the next/link, it is triggering a problem within the getInitialProps function

Currently, I am attempting to include a link that will direct users to the create page. However, when I tried clicking on the link, I encountered the following error: TypeError: Cannot read properties of undefined (reading 'cookies') The code fo ...

mongoose populate method does not return the expected results

My Project Objective I am currently in the process of creating a travel booking platform for a transportation company. One of the key features I am working on is displaying the name of the individual who made the booking on a specific page within the webs ...