Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes

Net Ninja · Intermediate ·🌐 Frontend Engineering ·8y ago

Key Takeaways

This video demonstrates how to use Vue and Firebase to build a real-time chat application, focusing on using props in routes for efficient data passing.

Full Transcript

okay so we have our welcome screen right now where we user types in their name and clicks enter chat now we need to figure out a way whereby we grab this name when they click this button and we push it into the next component the chat component so that the chat component knows what the name is so essentially what we'll be doing is passing in this name property into the chat component as a prop when we click this button and redirect them so we need to figure that out but first of all let's create the basics of this chat component so in the code let's create a new component by right-clicking over here go to a new file and call this chat dots of view okay so first of all we need a template and this template will be pretty simple to begin with a div with a class of chats and also a class of container okay now inside that we'll do an h2 and this will just say chat for now now we'll flesh out this template later but for now let's just do the script tag first of all and inside here we'll have our object the name is going to be chat and this needs to be a colon on that symbol okay and we also want to do our data function which returns an empty object we'll fill this in later okay finally let's do our style tag just in case we need to add any styles later okay so we have the basic component here for the chat window now I'd like to hook this up to our router so that if we go to forward slash chat we get this component right here so inside index J is inside the router file first of all we'll import that new component so import chat from and it's that's forward slash components forward slash chat okay now let's create our routes object for this component so a new object right here the path is going to be forward slash chat and the name of this route is going to be chat and finally the component will use is chat so now when a user goes to forward slash chat it should load up this component right here so let's save this and give it a whirl over here we'll go to forward slash chat and we see that component cool so this works okay so now we have this component set up what we'd like to do is when a user clicks on enter chat we take the name that they type in here and we pass it through into this chat component right here and redirect them to this component so first of all what we'll do is go into this component and we'll check that they first of all have in fact entered a name because we don't want to redirect them to the chat component if they've not entered a name yet so to do that inside this entered chat function will delete console dot log and we'll say if this name so if this name is null then we're not actually going to do this we'll do something else and the else will be to offer them some feedback now first of all we'll need to create a feedback property over here which will be null to begin with but if they try to click the button right here enter chat without inputting the name the else statement will run and we'll offer some feedback so we'll say this dot feedback is going to be now equal to you must enter a name to join okay now we want to output that feedback if they try to click this button without entering a name so we'll do that just above the button so we'll do a pee tag now this will only output if feedback is true now currently it's null which means it's false so we'll say V if on the P tag and we'll set it equal to feedback so for as long as this is null then we won't offer that feedback if it's not null then we will offer this feedback so we'll output it right here like so now we also want to give this a class of red text to make it red and stand out all right so first of all let's try this out if I now try to enter the chat without entering a name it's just gonna feed me back this information right here okay then so now if they do enter a name then we want to fire this bit right here and what we'd like to do is first of all redirect them to the chat component so let's do that first of all I'll say this dots router with a dollar before it remember this is how we access the router object to redirect users and then we redirect them by saying we want to push on to the router so this takes an object as a parameter this method and we can pass through some options into this object now one of those options is the name and that is the name of the component we want to redirect them to now we call the name of the chat component right here chat so let's grab that and paste it in right here okay so we're redirecting them to the chat component now now let's see if this works I'll just enter in any old name Mario and enter chat and we forwards denounced in the chat room but we still don't have access to that Mario name so we now need to figure out how to pass through that name Mario as a prop into this thing right here and there's a couple of steps to doing that first of all we need to go into our router file over here and inside this route object we need to tell it that we want to accept props as parameters when we redirect them into this route so the way we do that is by simply adding on a new property here called props and we'll set that to true so now when we enter this route right here it's expecting to have props passed to the new component okay now the prop we want to pass is the name so first of all we need to go into our chat component because when we pass props into a component remember we need to also accept them into components and to do that we need to add on a props property right here inside this component so the props that we want to accept are the name props or rather the name prop so now we've said to the router lot we want to plus a prop through to this component when we redirect them or go to this routes and we also inside the component are now accepting the prop called name so the last thing for us to do is actually pass this prop into this component when we redirect them now that is pretty simple to do we'll go to our welcome screen over here where we redirect them and after this name property we want to add another option now surprisingly this is not props as you might think it would be but in fact we use the params option right here now we've seen this before when we're passing through a route parameter right now we use the same option when we're passing through a prop as well it's just that now we've done a bit of extra configuration we've said that we're passing through props and we're accepting that prop because this time we're not passing through the parameter in the actual routes it's not going to be /ch at /the name of the person we're just passing it through as a prop which is not going to be seen inside the URL so the prop that we want to send is going to be called name because that's what we're expecting to accept right here the name and we'll set this equal to this name right here so we can access that by saying this dot name so we're redirecting them now to the chat component and we're passing through a parameter which is actually passing a prop and we've said that we want to accept that prop in here now this name right here name and this thing over here they must be the same thing because we're passing one thing and accepting one thing right they must equal the same thing so let's save that now and view it in a browser and see if this works we'll go back to the welcome screen and say Mario and in fact we can only find out if it works if we actually do something with that prop inside here so why not let's just output it this name because remember when we accept props and we get data like this inside a component we have access to them now by just saying this doc name much like we would have access to any data down here on this object so let's output this and Mario will be our name enter chap and now we can see chat Mario let's try a different one Luigi enter Chat Chat Luigi awesome and notice it's not that parameter up here it's not forward slash chat forward slash Luigi right here it's just forward slash chat and we're grabbing that name as a prop

Original Description

FULL COURSE LINK - https://www.udemy.com/build-web-apps-with-vuejs-firebase/?couponCode=NINJAYT18 Use Code NINJAYT18 for discount. Discount should already be applied when using the link above! See the full course for a full Vue.js tutorial, including building 3 projects with Firebase.
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 46 of 60

1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to use props in routes for efficient data passing in a Vue.js and Firebase real-time chat application. It covers the basics of routing and component communication in Vue.js.

Key Takeaways
  1. Create a new Vue.js project
  2. Set up Firebase real-time database
  3. Define routes with props
  4. Pass data between components using props
  5. Test the application
💡 Using props in routes allows for efficient data passing between components in a Vue.js application.

Related Reads

Up next
Host your website for free on GitHub Pages
Coder Coder
Watch →