In the following development, note that the coordinate variables do not appear explicitly and have no influence on the result.
Note that a vector is independent of the choice of coordinate system. Let's represent a vector $\vec V$ using two different systems with unit basis vectors $(\hat e_1,\hat e_2,\hat e_3)$ and $(\hat e_1',\hat e_2',\hat e_3')$.
Then, we can write $\vec V=\vec V'$ as
$$\begin{align}
\vec V&=\hat e_1V_1+\hat e_2V_2+\hat e_3V_3\\\\
&=\sum_{i=3}\hat e_iV_i \tag 1\\\\
=\vec V'&=\hat e_1'V_1'+\hat e_2'V_2'+\hat e_3'V_3'\\\\
&=\sum_{i=3}\hat e_i'V_i' \tag 2
\end{align}$$
We assume that each system is an orthogonal system, which means that $\hat e_i\cdot \hat e_j=\delta_{ij}$, where $\delta_{ij}$ is the Kronecker Delta and is equal to $1$ when $i=j$ and $0$ otherwise.
Forming the inner product of $\hat e_j$ with $(1)$ and $(2)$ and equating we find that
$$V_j=\sum_{i=1}^3 (\hat e_j\cdot \hat e_i')V_i' \tag 3$$
Now, let the unprimed system be the Cartesian system with unit basis vectors $((\hat e_1,\hat e_2,\hat e_3))=(\hat x,\hat y\hat z)$ and the primed system be the spherical system with unit basis vectors $(\hat e_1',\hat e_2',\hat e_3')=(\hat r,\hat \theta, \hat \phi)$.
Then, if $\hat e_j=\hat z$, then $(3)$ becomes
$$\begin{align}
V_z&=(\hat z\cdot \hat r)V_r+(\hat z\cdot \hat \theta)V_\theta+(\hat z\cdot \hat \phi)V_\phi\\\\
&=\cos(\theta)V_r-\sin(\theta)V_\theta
\end{align}$$
where we used $\hat z\cdot \hat e_i'=\cos(\text{angle between the unit vectors})$.
And we are done!