What causes a child to go from good eyesight to needing glasses in just a couple of years?

Growth. It doesn't take much, and children are growing at an amazing rate. A small change in the shape of the lens or the eyeball itself can make a difference. Plus, many kids compensate for a long time, and most likely eyesight was going bit by bit, but it was hard to notice until compensation was no longer possible.
Eye growth. Most children's eyes are too short at birth which makes them use their internal lense flexibility to adjust for vision needs. As they grow, so does their eye which neutralizes the shortness needs and eventually in many will start making them nearsighted needing glasses or contact lenses. There are more extremes of these which need earlier attention. It is not dietary or controllable.