Advanced apps with Vue CLI
Last updated
Was this helpful?
Last updated
Was this helpful?
Vue CLI (Command Line Interface) is a full system for rapid Vue.js development.
You can manually select what packages or features to be installed, options that are likely needed for more production-oriented projects.
Adding plugins to the project.
When a project is created, a structured folder hierarchy is created.
Also Single File Components are in place (with the .vue extension), meaning we'll be able to access the logic, the layout and the style within the same file.
What About the Separation of Concerns?
One important thing to note is that separation of concerns is not equal to the separation of file types. In modern UI development, instead of dividing the codebase into three huge layers that interweave with one another, it makes much more sense to divide them into loosely-coupled components and compose them. Inside a component, its template, logic and styles are inherently coupled, and collocating them actually makes the component more cohesive and maintainable.
In Vue.js, a view is a component.
There's no fundamental difference between a "view" and a "component" in terms of how Vue.js treats them. Both are built using the same component structure (with a template, script, and optional style).
So, why the distinction?
The distinction between "views" and "components" is primarily a matter of convention and organization within a Vue.js project. It's a way to structure your code and make it easier to understand.
Here's how the convention usually works:
Components: These are generally smaller, reusable UI elements that can be used across multiple parts of your application. Think of things like buttons, input fields, modals, or lists. They often focus on a specific piece of functionality or visual element.
Views: These are typically the top-level components that represent different "pages" or "screens" in your application. They are usually associated with specific routes and are responsible for composing and displaying multiple components to create the overall layout of a page.
Example:
Imagine an e-commerce application:
Components: ProductCard.vue
, ShoppingCart.vue
, AddToCartButton.vue
Views: HomePage.vue
, ProductDetailsPage.vue
, CheckoutPage.vue
Folder Structure
To reinforce this convention, Vue.js projects often have separate folders:
src/components/
: For reusable components.
src/views/
: For top-level view components.
Important Notes
Flexibility: This is just a convention, not a strict rule. You can structure your project in a way that makes sense to you.
Router Views: Vue Router (the official routing library for Vue.js) uses a special component called <router-view>
to dynamically display the component associated with the current route. This is where your "view" components are typically rendered.
We can use the default fetch
API or we can use a popular library such as Axios.
This Vue component, named AxiosExample
, fetches a list of posts from the JSONPlaceholder API upon mounting. It uses Axios to make an asynchronous GET request and manages three states in its template: "Loading data..." while the request is in progress, "Error fetching data..." if the request fails, and a list of post titles and bodies if the request is successful. The component updates its posts, loading, and error data properties to control which state is displayed to the user, ensuring a clear and informative UI during the data fetching process.
This Vue component, SimpleFormExample
, showcases basic form handling by using v-model
for two-way data binding on text inputs and a textarea, linking them to the formData
data object. Upon form submission (preventing default page reload), the handleSubmit
method copies the form data to submittedData
for display, clears the input fields by resetting formData
, and shows a basic submission alert.